-
Notifications
You must be signed in to change notification settings - Fork 722
support pnp resolver #1876
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
base: main
Are you sure you want to change the base?
support pnp resolver #1876
Changes from 16 commits
c315580
99dc36c
25b13ac
f24a170
b4aaac8
2373552
f09cda5
d00e36b
0a0f050
94a15b7
35d6188
35ee2ef
9463e11
dd08ae5
b368c1c
688455e
305fd49
f0ee55b
ef6d713
8d165ca
ae272f1
67752dc
1f06041
a9dbc68
df40d62
1485fe3
bfa05c3
e697dce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
require ( | ||
github.com/dlclark/regexp2 v1.11.5 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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) | ||
|
@@ -27,6 +30,7 @@ type compilerHost struct { | |
defaultLibraryPath string | ||
extendedConfigCache tsoptions.ExtendedConfigCache | ||
trace func(msg string) | ||
pnpResolutionConfig *pnp.ResolutionConfig | ||
} | ||
|
||
func NewCachedFSCompilerHost( | ||
|
@@ -49,12 +53,20 @@ func NewCompilerHost( | |
if trace == nil { | ||
trace = func(msg string) {} | ||
} | ||
|
||
pnpResolutionConfig := TryGetPnpResolutionConfig(currentDirectory) | ||
|
||
if pnpResolutionConfig != nil { | ||
fs = pnpvfs.From(fs) | ||
|
||
} | ||
|
||
return &compilerHost{ | ||
currentDirectory: currentDirectory, | ||
fs: fs, | ||
defaultLibraryPath: defaultLibraryPath, | ||
extendedConfigCache: extendedConfigCache, | ||
trace: trace, | ||
pnpResolutionConfig: pnpResolutionConfig, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package compiler | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compiler should not depend on these; any accesses it makes must go through a provided FS. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (#1911 will enforce this in the future) |
||
|
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pnp | ||
|
||
import "slices" | ||
|
||
var NodeJSBuiltins = []string{ | ||
jakebailey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"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) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.