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

BUG :: tsc-alias making relative a package.json defined lib #214

Closed
alanszp opened this issue Mar 6, 2024 · 2 comments
Closed

BUG :: tsc-alias making relative a package.json defined lib #214

alanszp opened this issue Mar 6, 2024 · 2 comments

Comments

@alanszp
Copy link

alanszp commented Mar 6, 2024

Context

package.json

"tsc-alias": "^1.8.8",

yarn.lock

tsc-alias@^1.8.8:
  version "1.8.8"
  resolved "https://registry.npmjs.org/tsc-alias/-/tsc-alias-1.8.8.tgz#48696af442b7656dd7905e37ae0bc332d80be3fe"
  integrity sha512-OYUOd2wl0H858NvABWr/BoSKNERw3N9GTi3rHPK8Iv4O1UyUXIrTTOAZNHsjlVpXFOhpJBVARI1s+rzwLivN3Q==
  dependencies:
    chokidar "^3.5.3"
    commander "^9.0.0"
    globby "^11.0.4"
    mylas "^2.1.9"
    normalize-path "^3.0.0"
    plimit-lit "^1.2.6"

Original TS file

// File: src/passport.ts

import GlobalPassport from "passport";

JS code (Compiled by TSC)

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("passport"));

tsc-alias output

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("./passport")); ----> CHECK HERE THAT MADE RELATIVE PASSPORT

Expectations

// File: dist/passport.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const passport_1 = __importDefault(require("passport")); ----> CHECK HERE. Just do not make passport relative

tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 20",
  "_version": "20.1.0",
  "include": ["./src/**/*", "./src/**/.*", "./*.js", "./.*.js"],
  "exclude": ["./dist/**/*", "./node_modules/**/*"],
  "compilerOptions": {
    "lib": ["es2022"],
    "module": "node16",
    "target": "es2022",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node16",
    "resolveJsonModule": true,
    "outDir": "./dist",
    "baseUrl": "./",

    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,

    "paths": {
      "@/*": ["src/*"]
    }
  }
}

More context

It's strange since I have other libs and default imports but it's only happening in passport import!

@alanszp
Copy link
Author

alanszp commented Mar 6, 2024

Debugging I found the problem!

I just renamed the file from src/passport.ts to src/passportInstance.ts and it stopped!

@izumiya @mgcrea @nicholas-ochoa @Jokero I'm closing this since I found the problem, but I would suggest trying to reproduce it, since it's a bug.

It seems that when you import a lib in a file with the same name as the lib (and maybe on the root of the project) it tries to add the ./

@alanszp alanszp closed this as completed Mar 6, 2024
@leo-fresha
Copy link

leo-fresha commented Aug 2, 2024

I think this should be reopened, as it seems to be an actual bug in tsc-alias.

I have encountered this while working on a backend typescript project.
My theory is that if a module is defined in a file with the same name as a node_modules dependency, and such module imports the node_modules dependency, the import is wrongly interpreted as to be relative (basically importing itself).

I have this code in a file scr/utils/zod.ts, which contains utility functions related to the Zod validation library.

// scr/utils/zod.ts
// defines extra helpers and utility functions for the zod library

import { z } from 'zod'  // import the library

export function arrayWithOnlyValidElements<T extends z.ZodTypeAny>(
  itemSchema: T,
) {
  const catchValue = {} as never;

  return z
    .array(itemSchema.catch(catchValue))
    .transform((arr) => arr.filter((el) => el !== catchValue))
    .catch([]);
}

After running tsc (with ES imports), the import is still correctly import { z } from 'zod.js'.
After running tsc-alias, a .js is added to the import:

> head -n 1 dist/utils/zod.js
import { z } from 'zod.js'

Which is incorrect. This is with tsc-alias 1.8.7.
@justkey007 is it possible to reopen this issue?

Having local modules with the same name as some dependency, in a subdir like utils or helpers, is quite a common pattern when writing code that is related to that dependency.

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

No branches or pull requests

2 participants