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

fix: corrects resolve for win32 paths #436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

davakh
Copy link

@davakh davakh commented Oct 30, 2024

fixes #401

accidentaly closed previous PR: #402

done();
});
});
it("should resolve namespaced module names", done => {
pnpApi.mocks.set("@user/pkg", path.resolve(fixture, "pkg"));
resolver.resolve({}, __dirname, "@user/pkg", {}, (err, result) => {
pnpApi.mocks.set(`@user${posixSep}pkg`, path.resolve(fixture, "pkg"));
Copy link
Author

@davakh davakh Oct 30, 2024

Choose a reason for hiding this comment

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

thoughts: i'm not sure about using posixSep in this place, because it should use osBasedPathSeparator (obps) based on reasoning from /test/util/path-separator description of this variables, but this test is mocking pnpapi and resolving it also in this file, so that's the place where I want to think about one more time. Probably, will try to take a look on pnpapi

absoluteOsBasedPath,
absoluteOsBasedResolvedPath,
posixSep: posixPathSeparator,
obps: osBasedPathSeparator,
Copy link
Author

@davakh davakh Oct 30, 2024

Choose a reason for hiding this comment

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

thoughts: I wanted to use fullname, but this entity being used too often in tests, so I decided to make name shorter

@@ -0,0 +1,108 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Copy link
Author

@davakh davakh Oct 30, 2024

Choose a reason for hiding this comment

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

question: not sure, what I should write here? should I add same author (Author Tobias Koppers @sokra) as other files? sorry, not so much contributing... 🙃

Copy link
Member

Choose a reason for hiding this comment

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

@davakh You can write own name here

// Functions below sometime handle transferPathToPosix and based on reading of test file you can see, that
// sometimes absolute paths started with obps (osBasedPathSeparator) and not absoluteOsBasedPath. That's because we use path.join too much in this test,
// and it works tricky on platforms:
// > path.posix.join('/abc/ab', '/abc/de') -> '/abc/ab/abc/de' (correct path after all, )
Copy link
Author

@davakh davakh Oct 30, 2024

Choose a reason for hiding this comment

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

nitpick: I'll fix it with following changes after review

@@ -199,7 +202,7 @@ function createOptions(options) {
}
}

return {
return normalizeOptionsForWindows({
Copy link
Member

Choose a reason for hiding this comment

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

I am afraid it will be slow on big builds...

Copy link
Author

@davakh davakh Nov 1, 2024

Choose a reason for hiding this comment

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

Okay, I'll take a look how it integrates with other libraries, maybe I'll find better place to normalize it. Don't really sure how often resolver factory can be called.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Why just don't coonvert it to unix path and work internaly only with /? I just don't understand some of the changes.

@@ -189,6 +189,8 @@ function getField(content, field) {
*/
function cdUp(directory) {
if (directory === "/") return null;
if (directory.match(/^[a-zA-Z]:$/)) return null;
Copy link
Member

Choose a reason for hiding this comment

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

Why we need it? We have unix path internally...

Copy link
Author

Choose a reason for hiding this comment

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

Because it can be win32 path still. Of course, it has posix char separator, but absolute path can be resolved as X:/something/ for example and for this case - to prevent this function to go up

@@ -77,10 +79,18 @@ module.exports = class PnpPlugin {
return;
}

if (typeof resolution === "string") {
resolution = transformPathToPosix(resolution);
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Copy link
Author

@davakh davakh Nov 1, 2024

Choose a reason for hiding this comment

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

external path that can be received from pnpapi -> we transform it to path with posix separators in case we get path with win32 separator from pnpapi

@@ -9,7 +9,6 @@
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */

const slashCode = "/".charCodeAt(0);
const backslashCode = "\\".charCodeAt(0);
Copy link
Member

Choose a reason for hiding this comment

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

Can you describe?

Copy link
Author

Choose a reason for hiding this comment

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

We can keep it like this, but I thought about removing it because internally we should handle only posix separator char

@@ -11,7 +11,7 @@
*/
module.exports = function getPaths(path) {
if (path === "/") return { paths: ["/"], segments: [""] };
const parts = path.split(/(.*?[\\/]+)/);
const parts = path.split(/(.*?[/]+)/);
Copy link
Member

Choose a reason for hiding this comment

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

And this

Copy link
Author

Choose a reason for hiding this comment

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

Same idea as this: #436 (comment)

@davakh
Copy link
Author

davakh commented Nov 1, 2024

Why just don't coonvert it to unix path and work internaly only with /? I just don't understand some of the changes.

That's what I did, that's how it works right now in this PR: we convert the external paths, we can get it in resolver or with ResolverFactory options and work internally only with posix / path separator.

So, actually, we convert win32 path to posix path only in case of ResolverFactory options or resolver usage. Yes, there can be some exceptions: f.e. PnpPlugin - it gets new paths from the pnpapi so it requires to handle this case separately.

UPDATE: Okay, I think that I got where the misunderstanding comes from: I'm thinking more about using win32 paths still but with posix path separator - because node.js supports this. So for me, it means we work with relative type of path (win32/posix) based on OS, but we have posix separator in win32 paths instead of win32 separator and it's still win32 type path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to handle win32 relative paths
3 participants