Skip to content

[wip] Build mode #1484

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 cmd/tsgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func runMain() int {
return runAPI(args[1:])
}
}
result := execute.CommandLine(newSystem(), args, false)
result := execute.CommandLine(newSystem(), args, nil)
return int(result.Status)
}
5 changes: 0 additions & 5 deletions cmd/tsgo/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ func (s *osSys) Writer() io.Writer {
return s.writer
}

func (s *osSys) EndWrite() {
// do nothing, this is needed in the interface for testing
// todo: revisit if improving tsc/build/watch unittest baselines
}

func newSystem() *osSys {
cwd, err := os.Getwd()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"strconv"
"sync"
"time"

"github.com/go-json-experiment/json"
"github.com/microsoft/typescript-go/internal/bundled"
Expand Down Expand Up @@ -472,3 +473,8 @@ func (s *Server) Stat(path string) vfs.FileInfo {
func (s *Server) Remove(path string) error {
panic("unimplemented")
}

// Chtimes implements vfs.FS.
func (s *Server) Chtimes(path string, aTime time.Time, mTime time.Time) error {
panic("unimplemented")
}
7 changes: 7 additions & 0 deletions internal/bundled/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (vfs *wrappedFS) Remove(path string) error {
return vfs.fs.Remove(path)
}

func (vfs *wrappedFS) Chtimes(path string, aTime time.Time, mTime time.Time) error {
if _, ok := splitPath(path); ok {
panic("cannot change times on embedded file system")
}
return vfs.fs.Chtimes(path, aTime, mTime)
}

type fileInfo struct {
mode fs.FileMode
name string
Expand Down
5 changes: 5 additions & 0 deletions internal/collections/syncset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ func (s *SyncSet[T]) Has(key T) bool {
return ok
}

func (s *SyncSet[T]) AddIfAbsent(key T) bool {
_, loaded := s.m.LoadOrStore(key, struct{}{})
return !loaded
}

func (s *SyncSet[T]) Add(key T) {
s.m.Store(key, struct{}{})
}
Expand Down
7 changes: 2 additions & 5 deletions internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ type emitter struct {
}

func (e *emitter) emit() {
if e.host.Options().ListEmittedFiles.IsTrue() {
e.emitResult.EmittedFiles = []string{}
}
// !!! tracing
e.emitJSFile(e.sourceFile, e.paths.JsFilePath(), e.paths.SourceMapFilePath())
e.emitDeclarationFile(e.sourceFile, e.paths.DeclarationFilePath(), e.paths.DeclarationMapPath())
Expand Down Expand Up @@ -254,7 +251,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
err := e.host.WriteFile(sourceMapFilePath, sourceMap, false /*writeByteOrderMark*/)
if err != nil {
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
} else if e.emitResult.EmittedFiles != nil {
} else {
e.emitResult.EmittedFiles = append(e.emitResult.EmittedFiles, sourceMapFilePath)
}
}
Expand All @@ -278,7 +275,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
}
if err != nil {
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
} else if e.emitResult.EmittedFiles != nil && !skippedDtsWrite {
} else if !skippedDtsWrite {
e.emitResult.EmittedFiles = append(e.emitResult.EmittedFiles, jsFilePath)
}

Expand Down
5 changes: 0 additions & 5 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,6 @@ func getInferredLibraryNameResolveFrom(options *core.CompilerOptions, currentDir
return tspath.CombinePaths(containingDirectory, "__lib_node_modules_lookup_"+libFileName+"__.ts")
}

type resolution struct {
node *ast.Node
resolvedModule *module.ResolvedModule
}

func getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, file *ast.SourceFile, meta ast.SourceFileMetaData, options *core.CompilerOptions) core.ResolutionMode {
if ref.ResolutionMode != core.ResolutionModeNone {
return ref.ResolutionMode
Expand Down
5 changes: 2 additions & 3 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func equalCheckJSDirectives(d1 *ast.CheckJsDirective, d2 *ast.CheckJsDirective)

func (p *Program) SourceFiles() []*ast.SourceFile { return p.files }
func (p *Program) Options() *core.CompilerOptions { return p.opts.Config.CompilerOptions() }
func (p *Program) GetRootFileNames() []string { return p.opts.Config.FileNames() }
func (p *Program) Host() CompilerHost { return p.opts.Host }
func (p *Program) GetConfigFileParsingDiagnostics() []*ast.Diagnostic {
return slices.Clip(p.opts.Config.GetConfigFileParsingDiagnostics())
Expand Down Expand Up @@ -1353,9 +1354,7 @@ func CombineEmitResults(results []*EmitResult) *EmitResult {
result.EmitSkipped = true
}
result.Diagnostics = append(result.Diagnostics, emitResult.Diagnostics...)
if emitResult.EmittedFiles != nil {
result.EmittedFiles = append(result.EmittedFiles, emitResult.EmittedFiles...)
}
result.EmittedFiles = append(result.EmittedFiles, emitResult.EmittedFiles...)
if emitResult.SourceMaps != nil {
result.SourceMaps = append(result.SourceMaps, emitResult.SourceMaps...)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/projectreferencedtsfakinghost.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compiler

import (
"strings"
"time"

"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
Expand Down Expand Up @@ -87,6 +88,11 @@ func (fs *projectReferenceDtsFakingVfs) Remove(path string) error {
panic("should not be called by resolver")
}

// Chtimes implements vfs.FS.
func (fs *projectReferenceDtsFakingVfs) Chtimes(path string, aTime time.Time, mTime time.Time) error {
panic("should not be called by resolver")
}

// DirectoryExists implements vfs.FS.
func (fs *projectReferenceDtsFakingVfs) DirectoryExists(path string) bool {
if fs.projectReferenceFileMapper.opts.Host.FS().DirectoryExists(path) {
Expand Down
15 changes: 15 additions & 0 deletions internal/core/buildoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core

type BuildOptions struct {
_ noCopy

Dry Tristate `json:"dry,omitzero"`
Force Tristate `json:"force,omitzero"`
Verbose Tristate `json:"verbose,omitzero"`
StopBuildOnErrors Tristate `json:"stopBuildOnErrors,omitzero"`

// CompilerOptions are not parsed here and will be available on ParsedBuildCommandLine

// Internal fields
Clean Tristate `json:"clean,omitzero"`
}
3 changes: 1 addition & 2 deletions internal/core/compileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type CompilerOptions struct {
AllowUnusedLabels Tristate `json:"allowUnusedLabels,omitzero"`
AssumeChangesOnlyAffectDirectDependencies Tristate `json:"assumeChangesOnlyAffectDirectDependencies,omitzero"`
AlwaysStrict Tristate `json:"alwaysStrict,omitzero"`
Build Tristate `json:"build,omitzero"`
CheckJs Tristate `json:"checkJs,omitzero"`
CustomConditions []string `json:"customConditions,omitzero"`
Composite Tristate `json:"composite,omitzero"`
Expand Down Expand Up @@ -142,7 +141,7 @@ type CompilerOptions struct {
Version Tristate `json:"version,omitzero"`
Watch Tristate `json:"watch,omitzero"`
ShowConfig Tristate `json:"showConfig,omitzero"`
TscBuild Tristate `json:"tscBuild,omitzero"`
Build Tristate `json:"build,omitzero"`
Help Tristate `json:"help,omitzero"`
All Tristate `json:"all,omitzero"`

Expand Down
4 changes: 2 additions & 2 deletions internal/core/projectreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ type ProjectReference struct {
}

func ResolveProjectReferencePath(ref *ProjectReference) string {
return resolveConfigFileNameOfProjectReference(ref.Path)
return ResolveConfigFileNameOfProjectReference(ref.Path)
}

func resolveConfigFileNameOfProjectReference(path string) string {
func ResolveConfigFileNameOfProjectReference(path string) string {
if tspath.FileExtensionIs(path, tspath.ExtensionJson) {
return path
}
Expand Down
8 changes: 4 additions & 4 deletions internal/diagnostics/diagnostics_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions internal/diagnostics/extraDiagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,21 @@
"A JSDoc '@type' tag may not occur with a '@param' or '@returns' tag.": {
"category": "Error",
"code": 8040
},
"Failed to delete file '{0}'.": {
"category": "Message",
"code": 6353
},
"Project '{0}' is out of date because config file does not exist.": {
"category": "Message",
"code": 6401
},
"Project '{0}' is out of date because input '{1}' does not exist.": {
"category": "Message",
"code": 6420
},
"Failed to update timestamp of file '{0}'.": {
"category": "Message",
"code": 5074
}
}
74 changes: 44 additions & 30 deletions internal/diagnosticwriter/diagnosticwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type FormattingOptions struct {
}

const (
foregroundColorEscapeGrey = "\u001b[90m"
ForegroundColorEscapeGrey = "\u001b[90m"
foregroundColorEscapeRed = "\u001b[91m"
foregroundColorEscapeYellow = "\u001b[93m"
foregroundColorEscapeBlue = "\u001b[94m"
Expand All @@ -39,43 +39,45 @@ func FormatDiagnosticsWithColorAndContext(output io.Writer, diags []*ast.Diagnos
if len(diags) == 0 {
return
}

for i, diagnostic := range diags {
if i > 0 {
fmt.Fprint(output, formatOpts.NewLine)
}
FormatDiagnosticWithColorAndContext(output, diagnostic, formatOpts)
}
}

if diagnostic.File() != nil {
file := diagnostic.File()
pos := diagnostic.Loc().Pos()
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
fmt.Fprint(output, " - ")
}
func FormatDiagnosticWithColorAndContext(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
if diagnostic.File() != nil {
file := diagnostic.File()
pos := diagnostic.Loc().Pos()
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
fmt.Fprint(output, " - ")
}

writeWithStyleAndReset(output, diagnostic.Category().Name(), getCategoryFormat(diagnostic.Category()))
fmt.Fprintf(output, "%s TS%d: %s", foregroundColorEscapeGrey, diagnostic.Code(), resetEscapeSequence)
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)
writeWithStyleAndReset(output, diagnostic.Category().Name(), getCategoryFormat(diagnostic.Category()))
fmt.Fprintf(output, "%s TS%d: %s", ForegroundColorEscapeGrey, diagnostic.Code(), resetEscapeSequence)
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)

if diagnostic.File() != nil && diagnostic.Code() != diagnostics.File_appears_to_be_binary.Code() {
fmt.Fprint(output, formatOpts.NewLine)
writeCodeSnippet(output, diagnostic.File(), diagnostic.Pos(), diagnostic.Len(), getCategoryFormat(diagnostic.Category()), "", formatOpts)
fmt.Fprint(output, formatOpts.NewLine)
}
if diagnostic.File() != nil && diagnostic.Code() != diagnostics.File_appears_to_be_binary.Code() {
fmt.Fprint(output, formatOpts.NewLine)
writeCodeSnippet(output, diagnostic.File(), diagnostic.Pos(), diagnostic.Len(), getCategoryFormat(diagnostic.Category()), "", formatOpts)
fmt.Fprint(output, formatOpts.NewLine)
}

if (diagnostic.RelatedInformation() != nil) && (len(diagnostic.RelatedInformation()) > 0) {
for _, relatedInformation := range diagnostic.RelatedInformation() {
file := relatedInformation.File()
if file != nil {
fmt.Fprint(output, formatOpts.NewLine)
fmt.Fprint(output, " ")
pos := relatedInformation.Pos()
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
fmt.Fprint(output, " - ")
WriteFlattenedDiagnosticMessage(output, relatedInformation, formatOpts.NewLine)
writeCodeSnippet(output, file, pos, relatedInformation.Len(), foregroundColorEscapeCyan, " ", formatOpts)
}
if (diagnostic.RelatedInformation() != nil) && (len(diagnostic.RelatedInformation()) > 0) {
for _, relatedInformation := range diagnostic.RelatedInformation() {
file := relatedInformation.File()
if file != nil {
fmt.Fprint(output, formatOpts.NewLine)
fmt.Fprint(output, " ")
pos := relatedInformation.Pos()
WriteLocation(output, file, pos, formatOpts, writeWithStyleAndReset)
fmt.Fprint(output, " - ")
WriteFlattenedDiagnosticMessage(output, relatedInformation, formatOpts.NewLine)
writeCodeSnippet(output, file, pos, relatedInformation.Len(), foregroundColorEscapeCyan, " ", formatOpts)
}
fmt.Fprint(output, formatOpts.NewLine)
}
}
}
Expand Down Expand Up @@ -196,7 +198,7 @@ func getCategoryFormat(category diagnostics.Category) string {
case diagnostics.CategoryWarning:
return foregroundColorEscapeYellow
case diagnostics.CategorySuggestion:
return foregroundColorEscapeGrey
return ForegroundColorEscapeGrey
case diagnostics.CategoryMessage:
return foregroundColorEscapeBlue
}
Expand Down Expand Up @@ -359,7 +361,7 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
}
return fmt.Sprintf("%s%s:%d%s",
fileName,
foregroundColorEscapeGrey,
ForegroundColorEscapeGrey,
line+1,
resetEscapeSequence,
)
Expand All @@ -383,3 +385,15 @@ func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatO
WriteFlattenedDiagnosticMessage(output, diagnostic, formatOpts.NewLine)
fmt.Fprint(output, formatOpts.NewLine)
}

func FormatDiagnosticsStatusWithColorAndTime(output io.Writer, time string, diag *ast.Diagnostic, formatOpts *FormattingOptions) {
fmt.Fprint(output, "[")
writeWithStyleAndReset(output, time, ForegroundColorEscapeGrey)
fmt.Fprint(output, "] ")
WriteFlattenedDiagnosticMessage(output, diag, formatOpts.NewLine)
}

func FormatDiagnosticsStatusAndTime(output io.Writer, time string, diag *ast.Diagnostic, formatOpts *FormattingOptions) {
fmt.Fprint(output, time, " - ")
WriteFlattenedDiagnosticMessage(output, diag, formatOpts.NewLine)
}
Loading
Loading