Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: pass relative config path to test-server for running#538

Merged
Skn0tt merged 8 commits into
microsoft:mainfrom
Skn0tt:fix-relative-config-path-testrun
Oct 11, 2024
Merged

fix: pass relative config path to test-server for running#538
Skn0tt merged 8 commits into
microsoft:mainfrom
Skn0tt:fix-relative-config-path-testrun

Conversation

@Skn0tt

@Skn0tt Skn0tt commented Oct 7, 2024

Copy link
Copy Markdown
Member

Closes microsoft/playwright#32959. Similar to the fix we shipped for debugging in #482 and #484.

@Skn0tt Skn0tt requested a review from dgozman October 7, 2024 10:39
@Skn0tt Skn0tt self-assigned this Oct 7, 2024
@Skn0tt

Skn0tt commented Oct 7, 2024

Copy link
Copy Markdown
Member Author

hmm, doesn't work on windows in my testing. looking more.

@Skn0tt Skn0tt marked this pull request as draft October 7, 2024 10:51
@Skn0tt

Skn0tt commented Oct 8, 2024

Copy link
Copy Markdown
Member Author

Found the culprit! The real root cause is microsoft/playwright#9193. It still exists, but is hard to solve on Playwright's side. We can work around it by normalising the cwd we use spawn the test-server in, and that's what f311510 does.

@Skn0tt Skn0tt marked this pull request as ready for review October 8, 2024 12:01
Comment thread src/playwrightTestServer.ts Outdated
@Skn0tt

Skn0tt commented Oct 10, 2024

Copy link
Copy Markdown
Member Author

I'll summarize the behaviour we're working around here for future visitors.

The Windows Filesystem is case-insensitive, but Node.js module loading is case-sensitive. That means that on Windows, C:\foo and c:\foo point to the same file, but on Node.js require-ing both of them will result in two instances of the file. This can lead to two instances of @playwright/test being loaded, which can't happen.

You can trigger this behaviour by running npx playwright test C:\tests\foo.spec.js c:\tests\bar.spec.js for example. Both of them import @playwright/test, but then one of them loads C:\node_modules\playwright and the other loads c:\node_modules\playwright. There's an existing comment in the code that describes this as This forks globals into 2 worlds.

Working around this is easy: Always specify file paths in the same case. #482 did this by making the paths relative.

There's another weird behaviour that plays into this, though. If the c:\foo.js file requires from node_modules, then the Node.js resolution algorithm will sometimes call realpath. This uses some Windows-specific implementation, and it just so happens that realpath normalises c:\node_modules\lib into C:\node_modules\lib. It uppercases the drive letter, leading to the same buggy behaviour we've seen before.

What does this mean? Never ever use paths with lowercase drive letters, and don't be in a CWD that has a lowercase drive letter.

Powershell realised this and made it impossible to go into a lowercase CWD, we did in #482 when we swapped absolute paths with lowercase drive letters for relative ones, and VS Code realised it when they normalize the program argument.

For some reason, this._model.config.workspaceFolder, which we get from VS Code, has a lower-case drive letter though. When we spawn a child process on that CWD in _startBackend, this leads to the bug described above. And this PR fixes it by uppercasing the drive letter.

Comment thread src/playwrightTestServer.ts Outdated
const wsEndpoint = await startBackend(this._vscode, {
args,
cwd: this._model.config.workspaceFolder,
cwd: this._ensureUpperCaseDriveLetter(cwd),

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.

  • Shouldn't we do the same at lines 241 and 253 new code, inside debugTests()?
  • Could line 360 new code that uses require.resolve() have the same issue?
  • What about all the same places in playwrightTestCLI.ts?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

  • no, because that's a call to VS Code. VS Code already performs the disk letter uppercasing for us, see the existing comment. I'll still update it for consistency though - can't hurt to normalize on our end.
  • it could have the same issue, yes. but it wouldn't manifest in the same way, because oopReporter doesn't import any Playwright singletons.
  • could have the same problems, but i'm less sure about it there because it uses configFolder as the CWD. Let's ship changes to that in a separate PR and release, so we can roll back more easily.

Comment thread src/playwrightTestServer.ts Outdated
Comment thread src/playwrightTestServer.ts Outdated
@Skn0tt

Skn0tt commented Oct 11, 2024

Copy link
Copy Markdown
Member Author

I've done one final check on my Windows machine, and this still fixes the issue. I'll merge and release this PR, and extend the patch to playwrightTestCLI.ts if we haven't heard any bug reports in a couple weeks.

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]: Error: Requiring @playwright/test second time when running some tests with VS Code Test Explorer

2 participants