Skip to content

Fix crash when @importing a Sass file that @uses another file #2599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Goodwine
Copy link
Member

@Goodwine Goodwine commented Jul 2, 2025

The crash happened when @importing a Sass file that @uses a user-defined module, only when the Sass file @includes a mixin at the root level if the mixin emits a declaration that would end up emitted at the root. This is allowed because the @import within a style rule would end up causing that top-level declaration to be wrapped by the style rule, and therefore won't be a top-level declaration. However, this crashed the compiler instead.

Fixes: #2588

Specs: sass/sass-spec#2060

@nex3
Copy link
Contributor

nex3 commented Jul 2, 2025

Please fix the CI

@Goodwine
Copy link
Member Author

Goodwine commented Jul 3, 2025

I believe sass-parser is broken at main rather than being broken by my PR

@Goodwine Goodwine force-pushed the import-use-crash branch from d521d43 to 320dd58 Compare July 9, 2025 18:09
@Goodwine Goodwine requested a review from nex3 July 9, 2025 18:12
@Goodwine Goodwine changed the title Fix crash when mixin @import with @use Fix crash when @importin a Sass file that @uses a user-defined module when the Sass file @includes a mixin at the root level when it emits a declaration Jul 9, 2025
Copy link
Contributor

@nex3 nex3 left a comment

Choose a reason for hiding this comment

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

Please make the commit message shorter. The Linux git style guide recommends 50 characters for the first line of a commit.

Comment on lines +1909 to 1910
_importParent = _parent;
_parent = _root;
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like the issue here is that _parent is reset to _root. What happens if we don't do that, rather than adding an extra variable to track (and then need to un-track) a different type of parent?

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like this is used to track ordering when doing @import containing @use.

e.g.

// This file is @imported into main.scss
@use 'uptream'
.foo-midstream{}

Results in

.foo-midstream{}
.foo-upstream{}

Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the existence of _parent cause that?

@Goodwine Goodwine changed the title Fix crash when @importin a Sass file that @uses a user-defined module when the Sass file @includes a mixin at the root level when it emits a declaration Fix crash when @importing a Sass file that @uses another file Jul 17, 2025
@Goodwine
Copy link
Member Author

I updated the tests but I'm still not clear whether @include meta.load-css() should pass and fail exactly like @import whether it's nested or not respectively. Currently it fails in both cases without crashing

@Goodwine Goodwine requested a review from nex3 July 17, 2025 18:24
@Goodwine
Copy link
Member Author

I updated the tests but I'm still not clear whether @include meta.load-css() should pass and fail exactly like @import whether it's nested or not respectively. Currently it fails in both cases without crashing

discussed offline, it should fail regardless. It is only @import that gets treated differently. So it already works fine. I will just update the test.

I'll also try to figure out why was _parent introduced in the first place

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.

Crash when @importing an upstream file with a top level @include that outputs a declaration only when that upstream file @uses another file
2 participants