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

Typescript @strapi/typescript-utils's getConfigPath return false negative due to incorrect directory checking procedure in Windows #14306

Closed
rachasakr opened this issue Sep 3, 2022 · 9 comments
Labels
status: duplicate Is a duplicate of another issue

Comments

@rachasakr
Copy link

Bug report

Required System information

  • Node.js version: 16.16.0
  • NPM version: 8.11.0
  • Strapi version: 4.3.6
  • Database: Postgres
  • Operating system: Windows 10

Describe the bug

When starting Strapi with a Typescript-enabled project, in the getConfigPath function, dirAbsolutePath = path.resolve(dir) return backward slash but typescript's ts.findConfigFile return forward slash. So when checking directory using configFilePath.startsWith, instead of true, it return false even if tsconfig.json is exists in both root and admin folder.

Steps to reproduce the behavior

  1. Create Strapi project with Typescript support and create tsconfig.json according to Strapi documentation
  2. yarn
  3. yarn start
  4. See error

Expected behavior

getConfigPath should return true if tsconfig.json is exists.

Screenshots

image

PS <project folder>\app> yarn strapi start
yarn run v1.22.19
warning ..\..\..\..\package.json: No license field
$ strapi start
getConfigPath <project folder>\app tsconfig.json <project folder>/app/tsconfig.json
<project folder>\app isTSProject false
getConfigPath <project folder>\app tsconfig.json <project folder>/app/tsconfig.json
getConfigPath <project folder>\app tsconfig.json <project folder>/app/tsconfig.json
getConfigPath <project folder>\app\src\admin tsconfig.json <project folder>/app/src/admin/tsconfig.json
[2022-09-03 22:31:53.588] debug: ⛔️ Server wasn't able to start properly.
[2022-09-03 22:31:53.591] error: Unknown dialect undefined
Error: Unknown dialect undefined
    at getDialectClass (<project folder>\app\node_modules\@strapi\database\lib\dialects\index.js:12:13)
    at getDialect (<project folder>\app\node_modules\@strapi\database\lib\dialects\index.js:19:23)
    at new Database (<project folder>\app\node_modules\@strapi\database\lib\index.js:27:20)
    at Function.Database.init (<project folder>\app\node_modules\@strapi\database\lib\index.js:77:35)
    at Strapi.bootstrap (<project folder>\app\node_modules\@strapi\strapi\lib\Strapi.js:392:30)
    at Strapi.load (<project folder>\app\node_modules\@strapi\strapi\lib\Strapi.js:457:16)
    at async Strapi.start (<project folder>\app\node_modules\@strapi\strapi\lib\Strapi.js:198:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Code snippets

Modified from get-config-path.js

module.exports = (dir, { filename = DEFAULT_TS_CONFIG_FILENAME, ancestorsLookup = false } = {}) => {
  const dirAbsolutePath = path.resolve(dir);
  const configFilePath = ts.findConfigFile(dirAbsolutePath, ts.sys.fileExists, filename);
  console.log('getConfigPath', dirAbsolutePath, filename, configFilePath);
...

Potential solution

Instead of String.startsWith, use path.normalize first.

@rachasakr
Copy link
Author

FYI, I've updated the return line to this and it's now working properly:

return path.normalize(configFilePath).startsWith(dirAbsolutePath) ? configFilePath : undefined;

@rachasakr rachasakr changed the title Typescript @strapi/typescript-utils's getConfigPath return false negative due to incorrect directory path in Windows Typescript @strapi/typescript-utils's getConfigPath return false negative due to incorrect directory checking procedure in Windows Sep 3, 2022
@rachasakr
Copy link
Author

rachasakr commented Sep 3, 2022

Searching further in typescript's repo, it seems they strictly use forward slash regardless of OS. So we have to either give in with TypeScrpt or do the path normalizing mentioned above.

@rachasakr
Copy link
Author

PS: This issue might duplicate with #14088. But you might consider the solution mentioned above.

@palbrecht1
Copy link

Thanks a lot!!

@strapi-bot
Copy link

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/strapi-custom-service-types/21977/1

@derrickmehaffy
Copy link
Member

In my attempt to reproduce, got the issue straight away on Windows 10

image

image

@derrickmehaffy
Copy link
Member

Also related to internal TID1832 for an EE customer

@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product status: confirmed Confirmed by a Strapi Team member or multiple community members source: typescript Source is related to TypeScript (typings, tooling, ...) labels Sep 16, 2022
@derrickmehaffy
Copy link
Member

duplicate: #13237

@derrickmehaffy derrickmehaffy added status: duplicate Is a duplicate of another issue and removed issue: bug Issue reporting a bug severity: high If it breaks the basic use of the product status: confirmed Confirmed by a Strapi Team member or multiple community members source: typescript Source is related to TypeScript (typings, tooling, ...) labels Sep 19, 2022
@kadimkareem
Copy link

FYI, I've updated the return line to this and it's now working properly:

return path.normalize(configFilePath).startsWith(dirAbsolutePath) ? configFilePath : undefined;

it works on local, thanks , but what if i deploy the project on a host machine ? the module folder will removed and new one will be created on the host with the same error ? every time i deploy the code i should edit it manually ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate Is a duplicate of another issue
Projects
Archived in project
Development

No branches or pull requests

5 participants