Skip to content

SG-40807 Fix raise FileNotFoundError#1088

Open
carlos-villavicencio-adsk wants to merge 2 commits intomasterfrom
ticket/SG-40807_fix_raise_FileNotFoundError
Open

SG-40807 Fix raise FileNotFoundError#1088
carlos-villavicencio-adsk wants to merge 2 commits intomasterfrom
ticket/SG-40807_fix_raise_FileNotFoundError

Conversation

@carlos-villavicencio-adsk
Copy link
Contributor

Root Cause Analysis

This seems to be a non-backwards-compatible regression introduced on #1047. The issue is in import_handler.py's find_spec method (lines 278-296). When a module like QtPrintSupport is requested from sgtk.platform.qt5:

  1. os.path.isdir(.../qt5/QtPrintSupport) returns False (no such directory)
  2. A SourceFileLoader is created pointing to .../qt5/QtPrintSupport.py — this file doesn't exist, but SourceFileLoader doesn't validate at creation time
  3. spec_from_loader() also doesn't validate — no exception is raised
  4. find_spec returns a valid-looking spec, claiming it "found" the module
  5. Python's import machinery uses the spec's SourceFileLoader to execute the module
  6. SourceFileLoader.exec_module() tries to read the non-existent file → FileNotFoundError
    The old imp-based code never had this problem because imp.find_module() would raise ImportError immediately if the file wasn't found.

What happens with the fix:

  1. Python calls CoreImportHandler.find_spec('sgtk.platform.qt5.QtPrintSupport', ...)
  2. The handler resolves the parent sgtk.platform.qt5 from sys.modules and gets its __path__
  3. It computes module_file = .../qt5/QtPrintSupport.py
  4. New check: os.path.isfile(module_file) → False → returns None
  5. Python tries remaining finders (PathFinder etc.) — none find QtPrintSupport either
  6. _find_and_load_unlocked gets spec = None → raises ModuleNotFoundError (subclass of ImportError)
    So the customer's except ImportError: block will correctly catch the error again, just like before the imp → importlib migration.

@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.42%. Comparing base (c7eb43e) to head (e1bc6df).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1088      +/-   ##
==========================================
+ Coverage   79.40%   79.42%   +0.02%     
==========================================
  Files         198      198              
  Lines       20738    20744       +6     
==========================================
+ Hits        16466    16475       +9     
+ Misses       4272     4269       -3     
Flag Coverage Δ
Linux 78.88% <100.00%> (+0.02%) ⬆️
Python-3.10 79.23% <100.00%> (+0.02%) ⬆️
Python-3.11 79.14% <100.00%> (+0.03%) ⬆️
Python-3.13 79.13% <100.00%> (+0.02%) ⬆️
Python-3.9 79.19% <100.00%> (+0.01%) ⬆️
Windows 78.91% <100.00%> (+<0.01%) ⬆️
macOS 78.89% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@carlos-villavicencio-adsk carlos-villavicencio-adsk marked this pull request as ready for review February 24, 2026 15:40
Copy link
Member

@julien-lang julien-lang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants