Skip to content

Don't inject import statements in package recipes#47947

Merged
tgamblin merged 1 commit intospack:developfrom
alalazo:fix/no-injected-import
Dec 5, 2024
Merged

Don't inject import statements in package recipes#47947
tgamblin merged 1 commit intospack:developfrom
alalazo:fix/no-injected-import

Conversation

@alalazo
Copy link
Copy Markdown
Member

@alalazo alalazo commented Dec 5, 2024

Remove a hack done by in RepoLoader, which was injecting an extra

from spack.package import *

at the beginning of each package.py.

Remove a hack done by RepoLoader, which was injecting an extra
```
from spack.package import *
```
at the beginning of each package.py
@spackbot-app spackbot-app bot added commands core PR affects Spack core functionality tests General test capability(ies) labels Dec 5, 2024
@tgamblin tgamblin self-requested a review December 5, 2024 20:40
@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Dec 5, 2024

I mean you can say it was done by me since I did it -- no need to anthropomorphize RepoLoader 😝

@tgamblin tgamblin merged commit 112e47c into spack:develop Dec 5, 2024
@alalazo alalazo deleted the fix/no-injected-import branch December 5, 2024 21:01
@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Dec 5, 2024

I mean you can say it was done by me since I did it

Or I can say that after 9 p.m. my English writing skills are 📉 😆

wdconinc added a commit to eic/eic-spack that referenced this pull request Dec 7, 2024
### Briefly, what does this PR introduce?
This PR ensures that all packages have a `from spack.package import *`
line since it is not injected by spack anymore. Ref:
spack/spack#47947

### What kind of change does this PR introduce?
- [x] Bug fix (issue #__)
- [ ] New feature (issue #__)
- [ ] Documentation update
- [ ] Other: __

### Please check if this PR fulfills the following:
- [ ] Tests for the changes have been added
- [ ] Documentation has been added / updated
- [ ] Changes have been communicated to collaborators

### Does this PR introduce breaking changes? What changes might users
need to make to their code?
No.

### Does this PR change default behavior?
No.
@wdconinc
Copy link
Copy Markdown
Contributor

wdconinc commented Dec 7, 2024

Ref #39380 (comment). There still was no actual deprecation warning printed by spack on import of packages without this import, so this may still surprise many 3rd party repo owners.

@alalazo
Copy link
Copy Markdown
Member Author

alalazo commented Dec 7, 2024

@wdconinc On the other hand:

$ spack style --fix

should fix them automatically. Should we catch that kind of error, and suggest to run the command?

@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Dec 7, 2024

Can we add something to the importer to warn that the import is needed?

@wdconinc
Copy link
Copy Markdown
Contributor

wdconinc commented Dec 7, 2024

With spack style --fix, a line import spack.spec is added, but that does not seem sufficient.

The exception appears as a NameError, and could be caught at e.g.

diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py
index c4a104c6b9..be5234898b 100644
--- a/lib/spack/spack/repo.py
+++ b/lib/spack/spack/repo.py
@@ -1240,6 +1240,13 @@ def get_pkg_class(self, pkg_name: str) -> Type["spack.package_base.PackageBase"]
                 module = importlib.import_module(fullname)
         except ImportError:
             raise UnknownPackageError(fullname)
+        except NameError as e:
+            msg = (
+                f"cannot load package '{pkg_name}' from the '{self.namespace}' repository: {e} " +
+                f"(this may be due to missing 'from spack.package import *' in {pkg_name}, which " +
+                f"can be fixed by running 'spack style --fix')"
+            )
+            raise RepoError(msg) from e
         except Exception as e:
             msg = f"cannot load package '{pkg_name}' from the '{self.namespace}' repository: {e}"
             raise RepoError(msg) from e

(Not sure how to get a filename from the info we have without creating the potential for more exceptions.)

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

Labels

commands core PR affects Spack core functionality tests General test capability(ies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants