Skip to content

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

@rachasakr

Description

@rachasakr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions