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

[miniflare] fix: allow relative modules scriptPath outside of CWD #4954

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Feb 8, 2024

Fixes #4721.

What this PR solves / how to test:

Previously, Miniflare would resolve relative scriptPaths against moduleRoot multiple times resulting in incorrect paths and module names. This would lead to can't use ".." to break out of starting directory workerd errors. This change ensures Miniflare uses scriptPath as is, and only resolves it relative to modulesRoot when computing module names. Note this bug didn't affect service workers. This allows you to reference a modules scriptPath outside the working directory with something like:

const mf = new Miniflare({
	modules: true,
	modulesRoot: "..",
	scriptPath: "../worker.mjs",
});

Notably, this wasn't a problem if modulesRoot and scriptPath were absolute paths.

Author has addressed the following:

  • Tests
    • Included
    • Not necessary because:
  • Changeset (Changeset guidelines)
    • Included
    • Not necessary because:
  • Associated docs
    • Issue(s)/PR(s):
    • Not necessary because: fixing a bug

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.

@mrbbot mrbbot requested a review from a team as a code owner February 8, 2024 15:14
Copy link

changeset-bot bot commented Feb 8, 2024

🦋 Changeset detected

Latest commit: 24a068a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
miniflare Patch
@cloudflare/pages-shared Patch
wrangler Patch

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);
Copy link
Contributor Author

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.

Copy link
Contributor

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().

Copy link
Contributor Author

@mrbbot mrbbot Feb 14, 2024

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. 👍

Copy link
Contributor

github-actions bot commented Feb 8, 2024

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 with this latest build directly:

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.


wrangler@3.27.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240129.1
workerd 1.20240129.0 1.20240129.0
workerd --version 1.20240129.0 2024-01-29

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@petebacondarwin petebacondarwin self-assigned this Feb 12, 2024
Copy link

codecov bot commented Feb 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0699506) 70.48% compared to head (24a068a) 70.49%.
Report is 384 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            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     

see 3 files with indirect coverage changes

const mf = new Miniflare({
modules: true,
modulesRoot: "..",
scriptPath: "../worker.mjs",
Copy link
Contributor

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?

Copy link
Contributor Author

@mrbbot mrbbot Feb 14, 2024

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);
Copy link
Contributor

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().

@mrbbot mrbbot merged commit 7723ac1 into main Feb 14, 2024
17 checks passed
@mrbbot mrbbot deleted the bcoll/fix-ancestor-module-root branch February 14, 2024 17:11
This was referenced Feb 14, 2024
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.

🐛 BUG: "can't use ".." to break out of starting directory" when using Service Bindings in monorepo
3 participants