microsoft / microsoft/TypeScript

`resolveJsonModule` doesn't copy source file when located within `node_modules` subdirectory

Offen
#43,940 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Module Resolution
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Bug Report

🔎 Search Terms

resolveJsonModule json node_modules

🕗 Version & Regression Information

Version 4.2.4
macOS 11.3

The issue seems very old, at least TS v3.4 is also affected.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about tsconfig.json
💻 Code

tsconfig.json

{
	"compilerOptions": {
		"baseUrl": "src",
		"rootDir": "src",
		"outDir": "dist",
		"resolveJsonModule": true,
	},
}

src/index.ts

import * as json from './config.json';

src/config.json

{}
🙁 Actual behavior

When I run tsc the output in dist contains index.js and config.json. This is good and expected. If any parent directory is named node_modules then the JSON file will no longer be copied. A full terminal session of this phenomenon is included below:

marcel@marcel ~/tmp $ ls -lah
total 0
drwxr-xr-x   3 marcel  staff    96B May  4 02:22 .
drwxr-xr-x  84 marcel  staff   2.6K May  4 02:22 ..
drwxr-xr-x   4 marcel  staff   128B May  4 02:21 test

marcel@marcel ~/tmp $ cd test 

marcel@marcel ~/tmp/test $ ls -lah
total 8
drwxr-xr-x  4 marcel  staff   128B May  4 02:21 .
drwxr-xr-x  3 marcel  staff    96B May  4 02:22 ..
drwxr-xr-x  4 marcel  staff   128B May  4 02:13 src
-rw-r--r--  1 marcel  staff   119B May  4 02:14 tsconfig.json

marcel@marcel ~/tmp/test $ npx tsc

marcel@marcel ~/tmp/test $ cat dist/config.json 
{}

marcel@marcel ~/tmp/test $ cd ..

marcel@marcel ~/tmp $ mkdir node_modules

marcel@marcel ~/tmp $ mv test node_modules 

marcel@marcel ~/tmp $ cd node_modules/test 

marcel@marcel ~/tmp/node_modules/test $ rm -rf dist 

marcel@marcel ~/tmp/node_modules/test $ npx tsc

marcel@marcel ~/tmp/node_modules/test $ cat dist/config.json
cat: dist/config.json: No such file or directory

marcel@marcel ~/tmp/node_modules/test $ ls dist 
index.js
🙂 Expected behavior

It is unexpected that the name of a parent directory would affect the compiler in this manner.

Additional information

If I invoke the compiler with the --traceResolution option the output is almost exactly the same but an additional line is logged in the node_modules parent case. The full log follows and the additional line is annotated inline. This diagnostic message does not appear when node_modules is found in the parent hierarchy.

======== Resolving module './config.json' from '/Users/marcel/tmp/node_modules/hello/test/src/index.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module as file / folder, candidate module location '/Users/marcel/tmp/node_modules/hello/test/src/config.json', target file type 'TypeScript'.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json.ts' does not exist.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json.tsx' does not exist.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json.d.ts' does not exist.
Directory '/Users/marcel/tmp/node_modules/hello/test/src/config.json' does not exist, skipping all lookups in it.
Loading module as file / folder, candidate module location '/Users/marcel/tmp/node_modules/hello/test/src/config.json', target file type 'JavaScript'.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json.js' does not exist.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json.jsx' does not exist.
Directory '/Users/marcel/tmp/node_modules/hello/test/src/config.json' does not exist, skipping all lookups in it.
Loading module as file / folder, candidate module location '/Users/marcel/tmp/node_modules/hello/test/src/config.json', target file type 'Json'.
File '/Users/marcel/tmp/node_modules/hello/test/src/config.json' exist - use it as a name resolution result.
File '/Users/marcel/tmp/node_modules/hello/package.json' does not exist.
  ^----- THIS LINE IS ADDED
======== Module name './config.json' was successfully resolved to '/Users/marcel/tmp/node_modules/hello/test/src/config.json'. ========

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem mit tsconfig.json, src/index.ts und src/config.json, indem du das Projekt unter einem übergeordneten node_modules-Verzeichnis platzierst. Führe zunächst tsc mit --traceResolution aus und vergleiche die erzeugten dist-Dateien und Diagnosen mit dem Projekt außerhalb von node_modules. Fertig ist es, wenn die aufgelöste JSON-Datei an beiden Orten nach dist kopiert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.