Skip to content

fix: generate command with running cross-device#3403

Merged
petebacondarwin merged 5 commits intocloudflare:mainfrom
Cherry:fix/cross-device-generate
Jun 27, 2023
Merged

fix: generate command with running cross-device#3403
petebacondarwin merged 5 commits intocloudflare:mainfrom
Cherry:fix/cross-device-generate

Conversation

@Cherry
Copy link
Copy Markdown
Contributor

@Cherry Cherry commented Jun 4, 2023

Fixes #2850

What this PR solves / how to test:

When using Windows (and maybe other operating systems), a renameSync can throw a Error: EXDEV: cross-device link not permitted error when renaming across drives. This is very common on Windows installs that may have Windows on C:/, but their user files on another drive like E:/.

This PR resolves that by first trying the renameSync (will be much faster than copying files on the same device, so worth doing first), but then catching the EXDEV error and handling it specifically afterwards.

For tests, I don't really know how to properly approach testing this cross-device behaviour if I'm honest - it'd need another filesystem (maybe mocked?) on some other virtual device - and most generate tests appear to be skipped right now anyway. If that's something folks would like to see, I'm going to need some guidance/examples on how best to do that testing. I ran manual tests locally on my Windows 10 machine using generate project-name https://github.com/cloudflare/workers-sdk/templates/experimental/worker-rust, as well as in WSL and on my Macbook just to ensure the previous behaviour wasn't impacted across a few OSes, and all worked well.

Author has included the following, where applicable:

Reviewer is to perform the following, as applicable:

  • Checked for inclusion of relevant tests
  • Checked for inclusion of a relevant changeset
  • Checked for creation of associated docs updates
  • Manually pulled down the changes and spot-tested

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jun 4, 2023

🦋 Changeset detected

Latest commit: 97ad9fa

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

This PR includes changesets to release 1 package
Name Type
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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jun 4, 2023

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/5390610863/npm-package-wrangler-3403

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/3403/npm-package-wrangler-3403

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/5390610863/npm-package-wrangler-3403 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/5390610863/npm-package-cloudflare-pages-shared-3403

Note that these links will no longer work once the GitHub Actions artifact expires.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 4, 2023

Codecov Report

Merging #3403 (97ad9fa) into main (607e032) will increase coverage by 0.03%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3403      +/-   ##
==========================================
+ Coverage   75.07%   75.11%   +0.03%     
==========================================
  Files         183      183              
  Lines       11083    11091       +8     
  Branches     2917     2918       +1     
==========================================
+ Hits         8321     8331      +10     
+ Misses       2762     2760       -2     
Impacted Files Coverage Δ
packages/wrangler/src/git-client.ts 85.71% <77.77%> (+4.46%) ⬆️

... and 1 file with indirect coverage changes

@Cherry
Copy link
Copy Markdown
Contributor Author

Cherry commented Jun 9, 2023

It seems like fs.cp is used in other places in the codebase, so it should be safe to use here too. I've gone ahead and removed the original dependency on fs-extra.

// @ts-expect-error non standard property on Error
if (err.code !== "EXDEV") {
logger.debug(err);
throw new Error(`Failed to find "${subdirectory}" in ${remote}`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps include the original err in the new Error?

Copy link
Copy Markdown
Contributor Author

@Cherry Cherry Jun 14, 2023

Choose a reason for hiding this comment

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

I'm happy to do this - the original code doesn't, so I assumed that was intentional, but if you think it's safe to just pass it through into the new error, can do!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm. Oh I see. OK let's leave it as-is.
(Aside, I note that subdirectory could actually be undefined that would be a weird message!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(Aside, I note that subdirectory could actually be undefined that would be a weird message!)

Yeah, it could for sure. The error handling here is pretty bare-bones. If you think it's worth refactoring here, let me know.

Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

I note that you are skipping the two unit tests. Do they not work?

I know you said that it was hard to create a real world test for this.
I wonder if we could create a network mapped drive that points to a local folder?
https://support.microsoft.com/en-us/windows/map-a-network-drive-in-windows-29ce55d1-34e3-a7e2-4801-131475f9557d#ID0EBD=Windows_11

@Cherry
Copy link
Copy Markdown
Contributor Author

Cherry commented Jun 14, 2023

I note that you are skipping the two unit tests. Do they not work?

Yeah I attempted a very dumb test by temporarily mocking renameSync to always throw the EXDEV error so the fs.cp codepath would be hit, but it didn't seem to stick. I'm not sure if runWrangler in tests is spawning a brand new process and therefore doesn't have the mocks? I'm really not sure, so left it skipped. Most tests in that file are skipped right now.

I wonder if we could create a network mapped drive that points to a local folder?
support.microsoft.com/en-us/windows/map-a-network-drive-in-windows-29ce55d1-34e3-a7e2-4801-131475f9557d#ID0EBD=Windows_11

This might work 🤔 I've never had to test anything like this in CI before. Prior libraries that have solved this seem to just handle it by naively mocking: https://github.com/andrewrk/node-mv/blob/645d8f4c783abf84204be704098fdd41f36ab195/test/test.js#L14

// likely on a different filesystem, so we need to copy instead of rename
// and then remove the original directory
try {
fs.cpSync(templatePath, targetDirectory);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We actually need a recursive option on this command too, which is why the tests were failing.
I have fixed this up and rebased.

@petebacondarwin petebacondarwin force-pushed the fix/cross-device-generate branch from 7dc55c7 to 97ad9fa Compare June 27, 2023 13:47
@petebacondarwin petebacondarwin requested review from a team as code owners June 27, 2023 13:47
Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Assuming CI is happy, I am happy.

@Cherry
Copy link
Copy Markdown
Contributor Author

Cherry commented Jun 27, 2023

Thanks Pete! 🎉

@petebacondarwin petebacondarwin merged commit 8d1521e into cloudflare:main Jun 27, 2023
@github-actions github-actions bot mentioned this pull request Jun 26, 2023
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: wrangler generate sometimes fails due to cross-device renaming

2 participants