Skip to content
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

fix(cli): properly handle errors #217

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

GeorgeTaveras1231
Copy link

@GeorgeTaveras1231 GeorgeTaveras1231 commented Jan 2, 2024

  • Handle all errors centrally and properly set process exit code
  • Use console.warn where appropriate to funnel messages to stderr
  • Do not ignore errors when loading config file

Details

I decided to open this PR after noticing some subtle issues in my build pipeline. I noticed that the typed-scss-modules build was silently failing - it couldn't load the config file because I had a broken import inside of it. Additionally, it continue trying to generate types after not being able to load the config, and it encountered another issue - it wasn't able to generate the types because it could not properly parse the sass files (because the configs were broken).

Example error:

[ typed-scss-modules ]: An error occurred loading the config file "<ROOT>/typed-scss-modules.config.js":
[ typed-scss-modules ]: ReferenceError: creaeSassEnhancedImporter is not defined
[ typed-scss-modules ]: Found 4 files. Generating type definitions...
[ typed-scss-modules ]: Can't find stylesheet to import.
[ typed-scss-modules ]:   ╷
[ typed-scss-modules ]: 1 │ @forward 'some-pkg' as some-pkg-*;
[ typed-scss-modules ]:   │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ typed-scss-modules ]:   ╵
[ typed-scss-modules ]:   src/__tests__/fixtures/forwarded-symbols/b.scss 1:1  @forward
[ typed-scss-modules ]:   src/__tests__/fixtures/forwarded-symbols/a.scss 1:1  root stylesheet (<ROOT>/__packages/docusaurus-sassdoc-plugin/src/__tests__/fixtures/forwarded-symbols/b.scss[1:1])
[ typed-scss-modules ]: Can't find stylesheet to import.
[ typed-scss-modules ]:   ╷
[ typed-scss-modules ]: 1 │ @forward 'some-pkg' as some-pkg-*;
[ typed-scss-modules ]:   │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ typed-scss-modules ]:   ╵
[ typed-scss-modules ]:   src/__tests__/fixtures/forwarded-symbols/b.scss 1:1  root stylesheet (<ROOT>/__packages/docusaurus-sassdoc-plugin/src/__tests__/fixtures/forwarded-symbols/b.scss[1:1])

TODO

  • Expose errors from sass.renderSync

Additional changes

- Handle all errors centrally and properly set process exit code
- Use console.warn where appropriate to funnel messaged to stderr
- Do not ignore errors when loading config file
George Taveras added 3 commits January 2, 2024 14:25
This stops ignoring errors generated by the sass compiler (as well as
any other potential errors generated during the process of writing to
disk). Instead, errors are now always thrown to the top of the stack,
they are logged, and properly set the exit code. Any errors that include
file info are assumed to be sass errors, and some additional formatting
applied to improve debuggability.
This also changes the TS target to ES2022 to support the second argument of the Error
constructor.
});

expect(fs.writeFileSync).toHaveBeenCalledTimes(6);
expect(fs.writeFileSync).toHaveBeenCalledTimes(9);
Copy link
Author

Choose a reason for hiding this comment

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

This number is higher because more files are now compiled, since the tool forces you to handle build errors. This is also why setting the aliases and aliasPrefix was required for this test.

Copy link
Owner

@skovy skovy left a comment

Choose a reason for hiding this comment

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

I think this makes sense to me. One question and a build error.


expect(result).toContain("No files found.");
expect(result.stderr.toString()).toContain("No files found.");
Copy link
Owner

Choose a reason for hiding this comment

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

why these changes?

Copy link
Author

Choose a reason for hiding this comment

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

@skovy this was needed because of the changes to use console.warn where appropriate. NodeJS sends console.warn messages to stderr (appropriately) -- additionally execSync doesn't expose messages in stderr of the downstream process. spawnSync however does let you inspect stdout and stderr

Copy link
Owner

Choose a reason for hiding this comment

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

Got it, that makes sense. While here and making this change, can we make them all spawnSync for consistency? and it sounds like it supports more functionality which we need here so others might need this in the future as well

@GeorgeTaveras1231
Copy link
Author

@skovy I've fixed the lint check. Let me know if I should fix anything else.

@skovy
Copy link
Owner

skovy commented Jan 19, 2024

should this be considered a breaking change? if some peoples builds are technically passing today, this could cause them to start failing?

@GeorgeTaveras1231
Copy link
Author

@skovy that's a good question - I would leave it up to you to decide ultimately. In the past I've chosen both of the options you have (push as a breaking change, or push as a patch).

Its totally reasonable to push this as a breaking change and it is the safer path.
But also totally justifiable to push this as a patch -- suggesting that these issues weren't intended to be in the current major.

I'm fine with either approach 😌

@skovy
Copy link
Owner

skovy commented Jan 28, 2024

My concern with merging this right now is that it sounds like without first finding a solution in #220, this will start to error for people without a fix?

I think it makes most sense if we first merge a solution to the other problem, then this PR?

@GeorgeTaveras1231
Copy link
Author

@skovy that's totally reasonable. Feel free to respond here or on the other PR but remind me what is left for the #220 to be considered mergeable? I still owe you some test cases 😌. And I responded to your last comment about the approach.

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.

None yet

2 participants