-
Notifications
You must be signed in to change notification settings - Fork 772
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
[miniflare] fix: allow relative modules scriptPath
outside of CWD
#4954
Conversation
🦋 Changeset detectedLatest commit: 24a068a The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@@ -174,8 +174,6 @@ export class ModuleLocator { | |||
} | |||
|
|||
visitEntrypoint(code: string, modulePath: string) { | |||
modulePath = path.resolve(this.modulesRoot, modulePath); |
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.
I think this change is safe to make. Resolving to the modulesRoot
root isn't correct here if modulePath
is relative. If modulePath
were absolute, this call would do nothing anyways.
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.
Right. scriptPath
should always be relative to cwd
(or script:NN
if provided inline). So if you wanted to force this to be absolute you would be more likely wanting to do only path.resolve(modulePath)
.
Interestingly, I assumed that this function would be recursive but it isn't. It is only called in getWorkerScript()
.
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.
Yep, the mutually recursive functions are #visitJavaScriptModule()
and #visitModule()
below. 👍
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7831766474/npm-package-wrangler-4954 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/4954/npm-package-wrangler-4954 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7831766474/npm-package-wrangler-4954 dev path/to/script.js Additional artifacts:npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7831766474/npm-package-create-cloudflare-4954 --no-auto-update npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7831766474/npm-package-miniflare-4954 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7831766474/npm-package-cloudflare-pages-shared-4954 Note that these links will no longer work once the GitHub Actions artifact expires.
Please ensure constraints are pinned, and |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #4954 +/- ##
==========================================
+ Coverage 70.48% 70.49% +0.01%
==========================================
Files 295 295
Lines 15407 15407
Branches 3948 3948
==========================================
+ Hits 10859 10861 +2
+ Misses 4548 4546 -2 |
const mf = new Miniflare({ | ||
modules: true, | ||
modulesRoot: "..", | ||
scriptPath: "../worker.mjs", |
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 we avoid the cwd change+resetting by choosing another tmp dir for the worker and using a relative path from the cwd to the other tmp dir?
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.
Yeah, we could. Was keen to test this behaviour using the CWD specifically since it's very simple, and is what users are most likely to hit. The useCwd()
implementation is actually from #4795, which uses it for some other tests too, so we're not just adding it for this. 👍
@@ -174,8 +174,6 @@ export class ModuleLocator { | |||
} | |||
|
|||
visitEntrypoint(code: string, modulePath: string) { | |||
modulePath = path.resolve(this.modulesRoot, modulePath); |
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.
Right. scriptPath
should always be relative to cwd
(or script:NN
if provided inline). So if you wanted to force this to be absolute you would be more likely wanting to do only path.resolve(modulePath)
.
Interestingly, I assumed that this function would be recursive but it isn't. It is only called in getWorkerScript()
.
Fixes #4721.
What this PR solves / how to test:
Previously, Miniflare would resolve relative
scriptPath
s againstmoduleRoot
multiple times resulting in incorrect paths and module names. This would lead tocan't use ".." to break out of starting directory
workerd
errors. This change ensures Miniflare usesscriptPath
as is, and only resolves it relative tomodulesRoot
when computing module names. Note this bug didn't affect service workers. This allows you to reference a modulesscriptPath
outside the working directory with something like:Notably, this wasn't a problem if
modulesRoot
andscriptPath
were absolute paths.Author has addressed the following:
Note for PR author:
We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label
highlight pr review
so future reviewers can take inspiration and learn from it.