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(ruleset-bundler): builtin module should not be treated as external #2109
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
ab335ba
to
6a36de1
Compare
667ec3e
to
8ea6cc3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test case for that?
We already have a few in there, so you can either try adapting it or just create a new one.
albeit tbh we should probably simply register |
The issue is that if spectral-cli bundles a remote ruleset, it will deem the imported package like '@stoplight/spectral-rulesets' as external package and fails, and if replacing it to a cdn url like "https://cdn.jsdelivr.net/npm/@stoplight/spectral-rulesets/+esm" , it also has issue . I already described it in #2107. |
already added |
@P0lip Kindly reminder, as this issue is blocking us using spectral in our pipeline. |
Hey, my apologies for the delay. The test you added works even without the change around externals. This is thanks to the use of |
Thanks for reviewing. It's weird, the unit test is similar to the usage in
|
Since my change is for the scenarios of the CLI or node, I think it will not impact the browser users. |
Node.js has a way to load modules over https, and they clearly state that loading local resources is disallowed in such a case - see "Cannot load non-network dependencies".
Skypack seems to bail out on a particular Ajv import, while jsdelivr isn't happy with lodash import
Taking a look now |
I took a look at the new test, and everything still works as expected. Here's an updated test showing that the code can be executed properly diff --git a/packages/ruleset-bundler/src/__tests__/index.jest.test.ts b/packages/ruleset-bundler/src/__tests__/index.jest.test.ts
index 4c92dac4..2ed04945 100644
--- a/packages/ruleset-bundler/src/__tests__/index.jest.test.ts
+++ b/packages/ruleset-bundler/src/__tests__/index.jest.test.ts
@@ -128,21 +128,23 @@ rules: {
plugins: [builtins(), commonjs(), ...node({ fs, fetch }), virtualFs(io)],
});
- expect(code).toContain(`var input = {
-extends: [spectralRulesets.oas],
-rules: {
- 'my-rule': {
- given: '$',
- then: {
- function: spectralFunctions.schema,
- functionOptions: {
- schema: {
- type: 'object',
+ const m: { exports?: unknown } = {};
+ Function('module', code)(m);
+ expect(m.exports).toEqual({
+ extends: [(await import('@stoplight/spectral-rulesets')).oas],
+ rules: {
+ 'my-rule': {
+ given: '$',
+ then: {
+ function: (await import('@stoplight/spectral-functions')).schema,
+ functionOptions: {
+ schema: {
+ type: 'object',
+ },
+ },
+ },
},
},
- },
- },
-},
-};`);
+ });
});
});
diff --git a/packages/ruleset-bundler/src/index.ts b/packages/ruleset-bundler/src/index.ts
index e4acb8c2..38acec50 100644
--- a/packages/ruleset-bundler/src/index.ts
+++ b/packages/ruleset-bundler/src/index.ts
@@ -36,10 +36,7 @@ export async function bundleRuleset(
: target === 'browser'
? id => isURL(id)
: (id, importer) =>
- id.startsWith('node:') ||
- (!isURL(id) &&
- isPackageImport(id) &&
- (importer === void 0 || !isURL(importer) || id.startsWith('@stoplight/spectral-'))),
+ id.startsWith('node:') || (!isURL(id) && isPackageImport(id) && (importer === void 0 || !isURL(importer))),
});
return (await bundle.generate({ format: format ?? (target === 'runtime' ? 'iife' : 'esm'), exports: 'auto' })) I'm still uncertain what exactly you're trying to achieve. Perhaps you could try to give me a ruleset you try to use? |
@P0lip what I want to fix is the CLI issue described in #2107 (comment), it's blocking us to run lint through CLI for a given remote ruleset which imports built in modules |
Sorry for updating the test case again, the previous version test has a subtle difference with the exact issue I encountered, I think it's caused by the commonjs statement |
I'm not clear on what the issue is with the tests, but I would like to chime in to say that I just recreated the problem using the current develop branch and then verified that this PR fixes it. Using this ruleset file:
here is the behavior I get when running with the current
Running with the fix in this PR, the linter runs successfully:
|
Ah, got it. I see now. So it's commonjs plugin messing things up. |
Thank you all for the feedback as well as the great test! I've provided a slightly fix over here #2174 |
Looks great, thanks so much |
Fixed in #2174 |
Fixes case 2 in #2107
Checklist
Does this PR introduce a breaking change?
Screenshots
If applicable, add screenshots or gifs to help demonstrate the changes. If not applicable, remove this screenshots section before creating the PR.
Additional context
Add any other context about the pull request here. Remove this section if there is no additional context.