justkey007 / tsc-alias

Replace alias paths with relative paths after typescript compilation
MIT License
902 stars 62 forks source link

Paths resolve to the wrong dir (tsc-alias debug: default replacer - Invalid path ) #134

Open mschering opened 2 years ago

mschering commented 2 years ago

Hi,

My problem is that ts-alias never replaces any import because it resolves to an invalid path. All paths resolve to /goui/dist/... instead of /goui/dist...

I have this tsconfig:

{
  "compilerOptions": {
    "strict" : true,
    "target": "ES2017",
    "sourceMap": true,
    "lib": ["es2020", "dom"],
    "moduleResolution": "node",
    "module":"es2020",
    "baseUrl": ".",
    "declaration": true,
    "declarationMap": true,
    "outDir": "./dist",
    "paths": {
      "@goui/*" : ["../goui/dist/*"]
    }
  }
}

Here's the debug output:

npx tsc-alias  --debug
tsc-alias debug: Loading config file: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/tsconfig.json'
tsc-alias debug: configDir '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core'
tsc-alias debug: loaded config (from file): {
  baseUrl: '.',
  outDir: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist',
  paths: { '@goui/*': [ '../goui/dist/*', [length]: 1 ] }
}
tsc-alias debug: loaded project config: {
  configFile: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/tsconfig.json',
  baseUrl: '.',
  outDir: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist',
  configDir: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core',
  outPath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist',
  confDirParentFolderName: 'groupoffice-core',
  hasExtraModule: false,
  configDirInOutPath: null,
  relConfDirPathInOutPath: null,
  pathCache: PathCache {
    useCache: true,
    existsCache: Map(0) {},
    absoluteCache: Map(0) {}
  }
}
tsc-alias debug: loaded full config: {
  configFile: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/tsconfig.json',
  baseUrl: '.',
  outDir: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist',
  configDir: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core',
  outPath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist',
  confDirParentFolderName: 'groupoffice-core',
  hasExtraModule: false,
  configDirInOutPath: null,
  relConfDirPathInOutPath: null,
  pathCache: PathCache {
    useCache: true,
    existsCache: Map(1) {
      '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist' => false
    },
    absoluteCache: Map(0) {}
  },
  output: Output {
    verb: false,
    debug: [Function (anonymous)] { [length]: 2, [name]: '' },
    [verbose]: [Setter]
  },
  aliasTrie: TrieNode {
    children: Map(1) {
      '@' => TrieNode {
        children: Map(1) {
          'g' => TrieNode {
            children: Map(1) {
              'o' => TrieNode {
                children: Map(1) {
                  'u' => TrieNode {
                    children: Map(1) {
                      'i' => TrieNode {
                        children: Map(1) {
                          '/' => TrieNode {
                            children: Map(0) {},
                            data: {
                              shouldPrefixMatchWildly: true,
                              prefix: '@goui/',
                              paths: [
                                {
                                  path: '../goui/dist/',
                                  isExtra: true,
                                  basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
                                },
                                [length]: 1
                              ]
                            }
                          }
                        },
                        data: null
                      }
                    },
                    data: null
                  }
                },
                data: null
              }
            },
            data: null
          }
        },
        data: null
      }
    },
    data: null
  },
  replacers: [ [length]: 0 ]
}
tsc-alias debug: Started loading replacers 
tsc-alias debug: Found node_modules: [ 'node_modules', '../../node_modules', [length]: 2 ]
tsc-alias debug: Added replacers to list from command-line filepaths: 
tsc-alias debug: Reading replacers config 
tsc-alias debug: Loading default replacer: [ 'default', { enabled: true }, [length]: 2 ]
tsc-alias debug: Replacer has no file: [ 'default', { enabled: true }, [length]: 2 ]
tsc-alias debug: Loading default replacer: [ 'base-url', { enabled: true }, [length]: 2 ]
tsc-alias debug: Replacer has no file: [ 'base-url', { enabled: true }, [length]: 2 ]
tsc-alias debug: Loaded replacers: [
  <ref *1> [Function: replaceImportStatement] {
    [length]: 1,
    [name]: 'replaceImportStatement',
    [prototype]: { [constructor]: [Circular *1] }
  },
  <ref *2> [Function: replaceBaseUrlImport] {
    [length]: 1,
    [name]: 'replaceBaseUrlImport',
    [prototype]: { [constructor]: [Circular *2] }
  },
  [length]: 2
]
tsc-alias debug: Search pattern: [
  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/**/*.{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}',
  '!/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/**/node_modules',
  [length]: 2
]
tsc-alias debug: Found files: [
  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/AuthManager.js',
  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/Login.js',
  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/User.js',
  [length]: 3
]
tsc-alias debug: Starting to replace file: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/AuthManager.js'
tsc-alias debug: Starting to replace file: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/Login.js'
tsc-alias debug: Starting to replace file: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/User.js'
tsc-alias debug: default replacer - requiredModule:  '@goui/jmap/Client.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  './Login.js'
tsc-alias debug: default replacer - alias:  
tsc-alias debug: base-url replacer - requiredModule:  '@goui/jmap/Client.js'
tsc-alias debug: base-url replacer - requiredModule:  './Login.js'
tsc-alias debug: base-url replacer - already resolved 
tsc-alias debug: replaced file without changes: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/AuthManager.js'
tsc-alias debug: replaced file without changes: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/User.js'
tsc-alias debug: default replacer - requiredModule:  '@goui/component/form/Form.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/Component.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/CardContainer.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/Translate.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/jmap/Client.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/form/Fieldset.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/form/TextField.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/Toolbar.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/Button.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/Notifier.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: default replacer - requiredModule:  '@goui/component/Window.js'
tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@goui/',
  paths: [
    {
      path: '../goui/dist/',
      isExtra: true,
      basePath: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/goui/dist'
tsc-alias debug: default replacer - Invalid path 
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/form/Form.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/Component.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/CardContainer.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/Translate.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/jmap/Client.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/form/Fieldset.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/form/TextField.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/Toolbar.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/Button.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/Notifier.js'
tsc-alias debug: base-url replacer - requiredModule:  '@goui/component/Window.js'
tsc-alias debug: replaced file without changes: '/Volumes/Projects/groupoffice-website/html/assets/account/vendor/groupoffice-core/dist/Login.js'

Am I doing something wrong or is it a bug?

Thanks!

Best regards, Merijn

Kolobok12309 commented 2 years ago

+1 bug with default replacer

{
  "baseUrl": ".",
  "outDir": "lib",
  "paths": [
    "#app": [
      "node_modules/nuxt/dist/app"
    ]
  ]
}

Alias after config.aliasTrie.search

{
  shouldPrefixMatchWildly: false,
  prefix: '#app',
  paths: [
    {
      path: 'node_modules/nuxt/dist/app',
      basePath: '/my/user/dir/projects/project-name/lib',
      isExtra: false
    }
  ]
}

After normalize path /my/user/dir/projects/project-name/lib

Can't say debug log, don't understand how enable it with programatically use

Have same alias for another module in node_modules, it worked, because normalize path try convert #kb/consts/error-code to /my/user/dir/projects/project-name/lib/consts/error-code and cannot find file(it doesn't exist)

{
  "#kb/*": [
        "node_modules/some-package/dist/*"
      ],
}
brainthinks commented 1 year ago

I ran into this issue today, and have a workaround that works in my project.

Here is the tsc-alias command i'm running:

tsc-alias --verbose --debug

Here is my relevant tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "@/shared/*": [ "../shared/*" ],
    },
  },
}

With the above configuration, i get the following debug output, indicating the failure:

tsc-alias debug: default replacer - alias:  {
  shouldPrefixMatchWildly: true,
  prefix: '@/shared/',
  paths: [
    {
      path: '../shared/',
      isExtra: true,
      basePath: '/path/to/project/dist/shared/shared'
    },
    [length]: 1
  ]
}
tsc-alias debug: default replacer - absoluteAliasPath:  '/path/to/project/dist/shared/shared'
tsc-alias debug: default replacer - Invalid path 

Of note in this debug output is that the shared dir appears twice in the basePath that gets calculated by tsc-alias. This problem only surfaced when we moved our shared dir from two levels up to one level up.

After many hours of troubleshooting, we realized that the configuration in the tsconfig.json works for our tsc command, but not for our tsc-alias command. Our solution was to provide 2 paths, one that would work with tsc, and one that would work with tsc-alias:

{
  "compilerOptions": {
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "@/shared/*": [
        // This is the correct path, which works with `tsc`
        "../shared/*",
        // This is the hacked path, which is necessary to enable `tsc-alias`.
        // This appears to be a bug in `tsc-alias` related to going up only
        // a single parent directory, since this didn't happen when our
        // shared dir was one level higher...
        // A value of `../*` works here too, but I decided to use a less
        // cryptic path.
        "../../shared/*",
      ],
    },
  },
}

Hopefully this helps someone.

michaelbayday commented 9 months ago

thank you @brainthinks that worked for us 🙏

parzhitsky commented 7 months ago

@brainthinks did a great job digging into the issue: the alias.basePath is constructed incorrectly.

@justkey007 Please, take a second look into this, whenever you have a spare time and motivation. Also, good job adding an informative debug system, it really helped.