Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/microsoft/typescript-go

go 1.25
go 1.25.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this.


require (
github.com/dlclark/regexp2 v1.11.5
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func processAllProgramFiles(
includeProcessor: &includeProcessor{},
}
loader.addProjectReferenceTasks(singleThreaded)
loader.resolver = module.NewResolver(loader.projectReferenceFileMapper.host, compilerOptions, opts.TypingsLocation, opts.ProjectName)
loader.resolver = module.NewResolver(loader.projectReferenceFileMapper.host, compilerOptions, opts.TypingsLocation, opts.ProjectName, opts.Host.GetPNPResolutionConfig())
for index, rootFile := range rootFiles {
loader.addRootTask(rootFile, nil, &fileIncludeReason{kind: fileIncludeKindRootFile, data: index})
}
Expand Down
16 changes: 16 additions & 0 deletions internal/compiler/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package compiler
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/module/pnp"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
"github.com/microsoft/typescript-go/internal/vfs/cachedvfs"
"github.com/microsoft/typescript-go/internal/vfs/pnpvfs"
)

type CompilerHost interface {
Expand All @@ -17,6 +19,7 @@ type CompilerHost interface {
Trace(msg string)
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
GetPNPResolutionConfig() *pnp.ResolutionConfig
}

var _ CompilerHost = (*compilerHost)(nil)
Expand All @@ -27,6 +30,7 @@ type compilerHost struct {
defaultLibraryPath string
extendedConfigCache tsoptions.ExtendedConfigCache
trace func(msg string)
pnpResolutionConfig *pnp.ResolutionConfig
}

func NewCachedFSCompilerHost(
Expand All @@ -49,12 +53,20 @@ func NewCompilerHost(
if trace == nil {
trace = func(msg string) {}
}

pnpResolutionConfig := TryGetPnpResolutionConfig(currentDirectory)

if pnpResolutionConfig != nil {
fs = pnpvfs.From(fs)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, this way I wouldn’t be able to take advantage of the cached VFS, so I’m thinking of moving its location instead.

}

return &compilerHost{
currentDirectory: currentDirectory,
fs: fs,
defaultLibraryPath: defaultLibraryPath,
extendedConfigCache: extendedConfigCache,
trace: trace,
pnpResolutionConfig: pnpResolutionConfig,
}
}

Expand Down Expand Up @@ -86,3 +98,7 @@ func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath.
commandLine, _ := tsoptions.GetParsedCommandLineOfConfigFilePath(fileName, path, nil, h, h.extendedConfigCache)
return commandLine
}

func (h *compilerHost) GetPNPResolutionConfig() *pnp.ResolutionConfig {
return h.pnpResolutionConfig
}
54 changes: 54 additions & 0 deletions internal/compiler/pnp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package compiler

import (
"io/fs"
"os"
"path/filepath"

module "github.com/microsoft/typescript-go/internal/module/pnp"
)

func TryGetPnpResolutionConfig(path string) *module.ResolutionConfig {
pnpManifestPath, err := findNearestPNPPath(path)
if err != nil {
return nil
}
pnpManifest, err := module.LoadPNPManifest(pnpManifestPath)
if err != nil {
return nil
}

return &module.ResolutionConfig{
Host: module.ResolutionHost{
FindPNPManifest: func(_ string) (*module.Manifest, error) {
return &pnpManifest, nil
},
},
}
}

func findNearestPNPPath(start string) (string, error) {
dir := start
if fi, err := os.Stat(start); err == nil {
if !fi.IsDir() {
dir = filepath.Dir(start)
}
} else {
dir = filepath.Dir(start)
}

for {
for _, name := range []string{".pnp.data.json", ".pnp.cjs", ".pnp.js"} {
candidate := filepath.Join(dir, name)
if fi, err := os.Stat(candidate); err == nil && !fi.IsDir() {
return candidate, nil
}
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return "", fs.ErrNotExist
}
5 changes: 5 additions & 0 deletions internal/execute/build/compilerHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/module/pnp"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand Down Expand Up @@ -38,3 +39,7 @@ func (h *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.Sourc
func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
return h.host.GetResolvedProjectReference(fileName, path)
}

func (h *compilerHost) GetPNPResolutionConfig() *pnp.ResolutionConfig {
return h.host.GetPNPResolutionConfig()
}
10 changes: 8 additions & 2 deletions internal/execute/build/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/execute/incremental"
"github.com/microsoft/typescript-go/internal/execute/tsc"
"github.com/microsoft/typescript-go/internal/module/pnp"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand All @@ -23,8 +24,9 @@ type host struct {
configTimes collections.SyncMap[tspath.Path, time.Duration]

// caches that stay as long as they are needed
resolvedReferences parseCache[tspath.Path, *tsoptions.ParsedCommandLine]
mTimes *collections.SyncMap[tspath.Path, time.Time]
resolvedReferences parseCache[tspath.Path, *tsoptions.ParsedCommandLine]
mTimes *collections.SyncMap[tspath.Path, time.Time]
pnpResolutionConfig *pnp.ResolutionConfig
}

var (
Expand Down Expand Up @@ -111,3 +113,7 @@ func (h *host) storeMTimeFromOldCache(file string, oldCache *collections.SyncMap
h.mTimes.Store(path, mTime)
}
}

func (h *host) GetPNPResolutionConfig() *pnp.ResolutionConfig {
return h.pnpResolutionConfig
}
62 changes: 62 additions & 0 deletions internal/module/pnp/builtins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pnp

import "slices"

var NodeJSBuiltins = []string{
"assert",
"assert/strict",
"async_hooks",
"buffer",
"child_process",
"cluster",
"console",
"constants",
"crypto",
"dgram",
"diagnostics_channel",
"dns",
"dns/promises",
"domain",
"events",
"fs",
"fs/promises",
"http",
"http2",
"https",
"inspector",
"module",
"net",
"os",
"path",
"path/posix",
"path/win32",
"perf_hooks",
"process",
"punycode",
"querystring",
"readline",
"readline/promises",
"repl",
"stream",
"stream/consumers",
"stream/promises",
"stream/web",
"string_decoder",
"sys",
"timers",
"timers/promises",
"tls",
"trace_events",
"tty",
"url",
"util",
"util/types",
"v8",
"vm",
"worker_threads",
"zlib",
}

func IsNodeJSBuiltin(s string) bool {
return slices.Contains(NodeJSBuiltins, s)
}
Binary file not shown.
3 changes: 3 additions & 0 deletions internal/module/pnp/fixtures/global-cache/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enableGlobalCache: true

nodeLinker: pnp
7 changes: 7 additions & 0 deletions internal/module/pnp/fixtures/global-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "global-cache",
"dependencies": {
"source-map-support": "^0.5.21"
},
"packageManager": "[email protected]"
}
1 change: 1 addition & 0 deletions internal/module/pnp/fixtures/global-cache/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(require.resolve('source-map-support'));
38 changes: 38 additions & 0 deletions internal/module/pnp/fixtures/global-cache/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"buffer-from@npm:^1.0.0":
version: 1.1.2
resolution: "buffer-from@npm:1.1.2"
checksum: 10c0/124fff9d66d691a86d3b062eff4663fe437a9d9ee4b47b1b9e97f5a5d14f6d5399345db80f796827be7c95e70a8e765dd404b7c3ff3b3324f98e9b0c8826cc34
languageName: node
linkType: hard

"global-cache@workspace:.":
version: 0.0.0-use.local
resolution: "global-cache@workspace:."
dependencies:
source-map-support: "npm:^0.5.21"
languageName: unknown
linkType: soft

"source-map-support@npm:^0.5.21":
version: 0.5.21
resolution: "source-map-support@npm:0.5.21"
dependencies:
buffer-from: "npm:^1.0.0"
source-map: "npm:^0.6.0"
checksum: 10c0/9ee09942f415e0f721d6daad3917ec1516af746a8120bba7bb56278707a37f1eb8642bde456e98454b8a885023af81a16e646869975f06afc1a711fb90484e7d
languageName: node
linkType: hard

"source-map@npm:^0.6.0":
version: 0.6.1
resolution: "source-map@npm:0.6.1"
checksum: 10c0/ab55398007c5e5532957cb0beee2368529618ac0ab372d789806f5718123cc4367d57de3904b4e6a4170eb5a0b0f41373066d02ca0735a0c4d75c7d328d3e011
languageName: node
linkType: hard
Binary file added internal/module/pnp/fixtures/left-pad-1.zip
Binary file not shown.
Binary file added internal/module/pnp/fixtures/left-pad-2.zip
Binary file not shown.
Loading