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 ts config path resolve on Windows #14080

Closed
wants to merge 1 commit into from

Conversation

florianmrz
Copy link
Contributor

What does it do?

This fixes the issue described in #14079

Why is it needed?

The check for an existing TS config does not work on Windows due to the paths being compared are using different path separators based on the OS in use. Since the POSIX style path will always be different than for example the same path on Windows, the check to see if they are included in one another fails.

How to test it?

As described in #14079: start a TS project on Windows.
The config detection will not work.

Related issue(s)/PR(s)

Related to #14079

@strapi-cla
Copy link

strapi-cla commented Aug 11, 2022

CLA assistant check
All committers have signed the CLA.

@Convly Convly self-requested a review August 22, 2022 09:37
@Convly Convly self-assigned this Aug 22, 2022
@Convly Convly added pr: fix This PR is fixing a bug source: typescript Source is related to TypeScript (typings, tooling, ...) labels Aug 22, 2022
@@ -16,7 +16,7 @@ const DEFAULT_TS_CONFIG_FILENAME = 'tsconfig.json';
* @return {string | undefined}
*/
module.exports = (dir, { filename = DEFAULT_TS_CONFIG_FILENAME, ancestorsLookup = false } = {}) => {
const dirAbsolutePath = path.resolve(dir);
const dirAbsolutePath = path.resolve(dir).split(path.sep).join(path.posix.sep);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm missing something, but I'm not sure to understand the need here. Isn't path.resolve supposed to return the path with os path separator anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does return it using the platform-specific path segment separator, correct.
This is why we need to manually fix this, as the result will be different depending on the OS we are on:

  • \ on Windows
  • / on POSIX

ts.findConfigFile() on line 20 always returns the path with POSIX style path separators (e.g. "foo/bar").
When we then compare it to the Windows style path that path.resolve() returns using .startsWith() in line 26, we are comparing "foo/bar" with "foo\bar" which will always return false.

This is what my change is doing, it ensures that the path is always using the POSIX style path separators, even on Windows. This is why this only fails on Windows and was most likely overlooked during development of this change.

Copy link
Contributor

@jramstedt jramstedt Sep 15, 2022

Choose a reason for hiding this comment

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

This is the correct fix.
I would also add ".split(path.sep).join(path.posix.sep);" to configFilePath just in case it changes in typescript in the future.
path.normalize() might also be needed.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the clarifications.

I'm now just wondering what happens if we try to compare the configFilePath (always posix) to the dirAbsolutePath (sep bound to the OS) on line 26. Shouldn't we do the check on two posix paths instead?

Copy link
Member

Choose a reason for hiding this comment

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

Basically for configFilePath.startsWith(dirAbsolutePath) with configFilePath as a / separated path and dirAbsolutePath with separators that depends on the OS, we can have mismatches, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue you describe is what is solved with the proposed change.

The path seperator of dirAbsolutePath was dependent on the OS of the host before, but by splitting it by the OS sep and joining with the POSIX sep (.split(path.sep).join(path.posix.sep)) we convert it into POSIX format, thus now comparing POSIX with POSIX style separators in line 26.

I didn't want to touch any other logic so this was the only change I made.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Convly What's holding us back from getting this merged? I feel like this should be fixed ASAP as it's preventing Windows users from starting any TS Strapi projects.

@Bassel17
Copy link
Member

Bassel17 commented Oct 5, 2022

Thank you a lot for your contribution to this PR
we decided to move forward with a different PR #14521

@Bassel17 Bassel17 closed this Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: fix This PR is fixing a bug source: typescript Source is related to TypeScript (typings, tooling, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants