-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
1132 lines (1016 loc) · 48.6 KB
/
Copy pathProgram.cs
File metadata and controls
1132 lines (1016 loc) · 48.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// =============================================================================
// Program.cs - Entry point for the SharpTS TypeScript interpreter/compiler
// =============================================================================
//
// Orchestrates the compiler pipeline: Lex → Parse → TypeCheck → (Interpret OR Compile)
//
// Usage modes:
// dotnet run - Start REPL (interactive mode)
// dotnet run -- <file.ts> - Interpret a TypeScript file
// dotnet run -- --compile <file.ts> - Compile to .NET IL assembly
// dotnet run -- -c <file.ts> -o out.dll - Compile with custom output path
//
// Global flags:
// -r, --reference <assembly.dll> - Add .NET assembly reference (repeatable; all modes).
// sharpts.json next to/above the entry script supplies
// project-level references and NuGet packages.
//
// Compilation flags:
// --ref-asm - Emit reference-assembly-compatible output
// --sdk-path <path> - Explicit path to .NET SDK reference assemblies
// --preserveConstEnums - Preserve const enum declarations
// --verify - Verify emitted IL using Microsoft.ILVerification
//
// Decorator flags:
// --experimentalDecorators - Enable Legacy (Stage 2) decorators
// --decorators - Enable TC39 Stage 3 decorators
// --emitDecoratorMetadata - Emit design-time type metadata
//
// Pipeline stages:
// 1. Lexer - Tokenizes source code into Token stream
// 2. Parser - Builds AST from tokens (with desugaring)
// 3. TypeChecker - Static type validation (runs before execution)
// 4. Interpreter - Tree-walking execution (default)
// OR
// 4. ILCompiler - Ahead-of-time compilation to .NET assembly (--compile flag)
//
// See also: Lexer.cs, Parser.cs, TypeChecker.cs, Interpreter.cs, ILCompiler.cs
// =============================================================================
using System.Reflection;
using SharpTS.Cli;
using SharpTS.Compilation;
using PEPacker;
using PEPacker.Bundling;
using SharpTS.Declaration;
using SharpTS.Diagnostics;
using SharpTS.Diagnostics.Exceptions;
using SharpTS.Execution;
using SharpTS.Modules;
using SharpTS.Packaging;
using SharpTS.Parsing;
using SharpTS.References;
using SharpTS.TypeSystem;
// Initialize fork IPC if this process was spawned via child_process.fork()
SharpTS.Runtime.Types.ForkIpcClient.TryInitialize();
// Parse command-line arguments
var parser = new CommandLineParser();
var command = parser.Parse(args);
switch (command)
{
case ParsedCommand.Help:
PrintHelp();
return;
case ParsedCommand.Version:
Console.WriteLine($"sharpts {GetVersion()}");
return;
case ParsedCommand.Error error:
Console.WriteLine(error.Message);
if (error.ShowCompileUsage)
PrintCompileUsage();
Environment.Exit(error.ExitCode);
break;
case ParsedCommand.Repl repl:
var replRefs = LoadDotNetReferences(Environment.CurrentDirectory, repl.Options.References);
if (replRefs.ManifestPath != null)
Console.WriteLine($"Loaded {replRefs.ManifestPath}: {replRefs.References.Count} assembly reference(s)");
RunPromptAsync(repl.Options.DecoratorMode).GetAwaiter().GetResult();
break;
case ParsedCommand.Script script:
RunFile(script.ScriptPath, script.Options.DecoratorMode, script.Options.EmitDecoratorMetadata, script.ScriptArgs, script.Options.CheckJs, script.Options.References);
break;
case ParsedCommand.Compile compile:
var outputOptions = new OutputOptions(compile.CompileOptions.MsBuildErrors, compile.CompileOptions.QuietMode, compile.CompileOptions.Standalone);
CompileFile(
compile.InputFile,
compile.OutputFile,
compile.CompileOptions.PreserveConstEnums,
compile.CompileOptions.UseReferenceAssemblies,
compile.CompileOptions.SdkPath,
compile.CompileOptions.VerifyIL,
compile.GlobalOptions.DecoratorMode,
compile.GlobalOptions.EmitDecoratorMetadata,
compile.PackOptions,
outputOptions,
compile.CompileOptions.References,
compile.CompileOptions.Target,
compile.CompileOptions.Bundler
);
break;
case ParsedCommand.GenDecl genDecl:
GenerateDeclarations(genDecl.TypeOrAssembly, genDecl.OutputPath, genDecl.Json, genDecl.References);
break;
}
/// <summary>
/// Resolves and loads third-party reference assemblies (sharpts.json manifest + -r flags)
/// before any module loading or type checking, so every resolution seam sees the types.
/// Reference errors are user-facing configuration problems: print and exit.
/// </summary>
static ReferenceSet LoadDotNetReferences(string startDirectory, IReadOnlyList<string> cliReferences)
{
try
{
return DotNetReferences.Load(startDirectory, cliReferences);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(1);
throw; // unreachable
}
}
static void RunFile(string path, DecoratorMode decoratorMode, bool emitDecoratorMetadata, string[]? scriptArgs = null, bool checkJs = false, IReadOnlyList<string>? references = null)
{
string absolutePath = Path.GetFullPath(path);
string source = File.ReadAllText(absolutePath);
// Third-party assembly references (sharpts.json walked up from the entry script,
// plus -r flags) load before any module resolution so dotnet: imports and
// @DotNetType declarations can see the types.
LoadDotNetReferences(Path.GetDirectoryName(absolutePath) ?? ".", references ?? []);
// Set script arguments for process.argv
SharpTS.Runtime.BuiltIns.ProcessBuiltIns.SetScriptArguments(absolutePath, scriptArgs ?? []);
// Lex to check for triple-slash path references
var lexer = new Lexer(source);
lexer.ScanTokens();
bool hasPathReferences = lexer.TripleSlashDirectives.Any(d => d.Type == TripleSlashReferenceType.Path);
// CommonJS files need module mode for require()/module.exports semantics
bool isCjsFile = SharpTS.Modules.CommonJsDetector.Detect(absolutePath)
== SharpTS.Modules.CommonJsDetector.ModuleKind.CommonJs
&& (source.Contains("require(") || source.Contains("module.exports") || source.Contains("exports."));
// Check if the file contains imports/exports or path references - if so, use module mode
if (hasPathReferences || source.Contains("import ") || source.Contains("export ") || isCjsFile)
{
RunModuleFile(absolutePath, decoratorMode, emitDecoratorMetadata, scriptArgs);
}
else
{
Run(source, decoratorMode, emitDecoratorMetadata, filePath: absolutePath, checkJs: checkJs);
}
}
static void RunModuleFile(string absolutePath, DecoratorMode decoratorMode, bool emitDecoratorMetadata, string[]? scriptArgs = null)
{
try
{
// Load the entry module and all dependencies
var resolver = new ModuleResolver(absolutePath);
var entryModule = resolver.LoadModule(absolutePath, decoratorMode);
var allModules = resolver.GetModulesInOrder(entryModule);
// Type checking across all modules (still uses Check-style API for modules)
// Module type checking has its own error handling
var checker = new TypeChecker();
checker.SetDecoratorMode(decoratorMode);
var typeMap = checker.CheckModules(allModules, resolver);
// Check for type errors — warnings (from lenient CJS modules) don't block execution
var diagnostics = checker.GetDiagnostics();
bool hasErrors = diagnostics.Any(d => d.Severity == SharpTS.Diagnostics.DiagnosticSeverity.Error);
if (hasErrors)
{
foreach (var d in diagnostics.Where(d => d.Severity == SharpTS.Diagnostics.DiagnosticSeverity.Error))
Console.WriteLine($"Error: {d}");
return;
}
// Interpretation
var interpreter = new Interpreter();
interpreter.SetDecoratorMode(decoratorMode);
// Program main interpreter: fire beforeExit/exit at event-loop drain and
// receive signal events (#1080/#1081).
interpreter.EmitProcessLifecycleEvents = true;
// If this process was forked, wire its IPC channel to this interpreter's loop so
// 'message' handlers run with an interpreter and the child stays alive (#1017).
SharpTS.Runtime.Types.ForkIpcClient.Instance?.AttachLoop(interpreter);
// Variable Resolution Phase (enables O(1) lookups)
var varResolver = new VariableResolver(interpreter);
foreach (var module in allModules)
{
if (!module.IsBuiltIn)
varResolver.Resolve(module.Statements);
}
interpreter.InterpretModules(allModules, resolver, typeMap);
// Node default: an unhandled promise rejection makes the process
// exit nonzero. The rejection itself was already reported to stderr
// by the interpreter when it was observed (#228).
if (interpreter.HadUnhandledRejection)
{
Environment.Exit(1);
}
}
catch (SharpTS.Runtime.Exceptions.ThrowException tex)
{
Console.WriteLine($"Error: {tex.Value}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static async Task RunPromptAsync(DecoratorMode decoratorMode)
{
PrintBanner();
if (decoratorMode != DecoratorMode.None)
{
Console.WriteLine($"Decorator mode: {decoratorMode}");
}
Console.WriteLine("Type expressions to evaluate. Press Ctrl+C to cancel input.");
Console.WriteLine("Type .help for available commands.");
Console.WriteLine();
var repl = new SharpTS.Repl.ReplEngine(decoratorMode);
await repl.RunAsync();
}
static void Run(string source, DecoratorMode decoratorMode, bool emitDecoratorMetadata = false, Interpreter? interpreter = null, string? filePath = null, bool checkJs = false)
{
interpreter ??= new Interpreter();
interpreter.SetDecoratorMode(decoratorMode);
// Program main interpreter: fire beforeExit/exit at event-loop drain and
// receive signal events (#1080/#1081).
interpreter.EmitProcessLifecycleEvents = true;
// Forked-child IPC: attach this interpreter's loop (idempotent — only acts once).
SharpTS.Runtime.Types.ForkIpcClient.Instance?.AttachLoop(interpreter);
Lexer lexer = new(source);
List<Token> tokens = lexer.ScanTokens();
Parser parser = new(tokens, decoratorMode);
var parseResult = parser.Parse();
if (!parseResult.IsSuccess)
{
foreach (var diagnostic in parseResult.Diagnostics)
Console.WriteLine($"Error: {diagnostic}");
if (parseResult.HitErrorLimit)
Console.WriteLine("Too many errors, stopping.");
return;
}
try
{
// Static Analysis Phase — skipped for .js files unless --check-js or
// // @ts-check opts in (matches tsc's checkJs:false default).
TypeMap? typeMap = null;
if (TypeCheckPolicy.ShouldTypeCheck(filePath, lexer.Pragmas, checkJsDefault: checkJs))
{
TypeChecker checker = new();
checker.SetDecoratorMode(decoratorMode);
var typeResult = checker.CheckWithRecovery(parseResult.Statements);
// Apply // @ts-ignore / @ts-expect-error line directives.
var filteredDiagnostics = TypeCheckPolicy.ApplyLineDirectives(typeResult.Diagnostics, lexer.Pragmas);
bool hasErrors = filteredDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error);
if (hasErrors)
{
foreach (var diagnostic in filteredDiagnostics)
Console.WriteLine($"Error: {diagnostic}");
if (typeResult.HitErrorLimit)
Console.WriteLine("Too many errors, stopping.");
Environment.Exit(1);
}
typeMap = typeResult.TypeMap;
}
// Variable Resolution Phase (enables O(1) lookups)
var resolver = new VariableResolver(interpreter);
resolver.Resolve(parseResult.Statements);
// Interpretation Phase
interpreter.Interpret(parseResult.Statements, typeMap);
// Node default: an unhandled promise rejection makes the process
// exit nonzero (#228).
if (interpreter.HadUnhandledRejection)
{
Environment.Exit(1);
}
}
catch (SharpTSException ex)
{
Console.WriteLine($"Error: {ex.Diagnostic}");
}
catch (SharpTS.Runtime.Exceptions.ThrowException tex)
{
Console.WriteLine($"Error: {tex.Value}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
static void CompileFile(string inputPath, string outputPath, bool preserveConstEnums, bool useReferenceAssemblies, string? sdkPath, bool verifyIL, DecoratorMode decoratorMode, bool emitDecoratorMetadata, PackOptions packOptions, OutputOptions outputOptions, IReadOnlyList<string> references, OutputTarget target, BundlerMode bundlerMode)
{
try
{
string absolutePath = Path.GetFullPath(inputPath);
string source = File.ReadAllText(absolutePath);
// Third-party assembly references (sharpts.json + -r) load into this process
// before module resolution: dotnet: imports resolve at module-load time, and
// the returned set drives the post-Save co-location of referenced DLLs.
var externalRefs = LoadDotNetReferences(Path.GetDirectoryName(absolutePath) ?? ".", references);
// Load package.json if packaging is enabled
PackageJson? packageJson = null;
AssemblyMetadata? metadata = null;
if (packOptions.Pack)
{
var inputDir = Path.GetDirectoryName(absolutePath) ?? ".";
packageJson = PackageJsonLoader.FindAndLoad(inputDir);
if (packageJson == null && packOptions.PackageIdOverride == null)
{
Console.WriteLine("Error: No package.json found. Provide --package-id and --version, or create a package.json.");
Environment.Exit(1);
}
// Create assembly metadata from package.json and overrides
if (packageJson != null)
{
metadata = AssemblyMetadata.FromPackageJson(packageJson);
if (!string.IsNullOrEmpty(packOptions.VersionOverride))
{
var versionPart = packOptions.VersionOverride.Split('-')[0];
if (Version.TryParse(versionPart, out var ver))
{
metadata = metadata with { Version = ver, InformationalVersion = packOptions.VersionOverride };
}
}
}
else
{
// Create minimal metadata from CLI overrides
Version? version = null;
if (!string.IsNullOrEmpty(packOptions.VersionOverride))
{
var versionPart = packOptions.VersionOverride.Split('-')[0];
Version.TryParse(versionPart, out version);
}
metadata = new AssemblyMetadata(
Version: version,
Title: packOptions.PackageIdOverride,
InformationalVersion: packOptions.VersionOverride
);
}
}
// Set up diagnostic reporter
var reporter = new DiagnosticReporter { MsBuildFormat = outputOptions.MsBuildErrors, QuietMode = outputOptions.QuietMode };
// Parse first to check for module statements and path references
Lexer lexer = new(source);
List<Token> tokens = lexer.ScanTokens();
Parser parser = new Parser(tokens, decoratorMode).WithFilePath(absolutePath);
var parseResult = parser.Parse();
if (!parseResult.IsSuccess)
{
reporter.ReportAll(parseResult.Diagnostics);
if (parseResult.HitErrorLimit)
Console.WriteLine("Too many errors, stopping.");
Environment.Exit(1);
}
var statements = parseResult.Statements;
// Check for path references (script files with references need module resolution)
bool hasPathReferences = lexer.TripleSlashDirectives.Any(d => d.Type == TripleSlashReferenceType.Path);
// Check AST for import/export statements or path references
// Include ImportRequire for CommonJS-style: import X = require('./module')
bool hasModules = hasPathReferences || statements.Any(s => s is Stmt.Import or Stmt.Export or Stmt.ImportRequire);
// CommonJS files (.cjs, or .js classified as CJS) also need module-mode compilation
// because they use the CJS module pipeline (per-file class with $exports field).
if (!hasModules)
{
bool isCjsFile = SharpTS.Modules.CommonJsDetector.Detect(absolutePath)
== SharpTS.Modules.CommonJsDetector.ModuleKind.CommonJs
&& (source.Contains("require(") || source.Contains("module.exports") || source.Contains("exports."));
if (isCjsFile)
{
hasModules = true;
}
}
if (hasModules)
{
CompileModuleFile(absolutePath, outputPath, preserveConstEnums, useReferenceAssemblies, sdkPath, verifyIL, decoratorMode, outputOptions, metadata, references, target, bundlerMode, externalRefs);
}
else
{
CompileSingleFile(statements, outputPath, preserveConstEnums, useReferenceAssemblies, sdkPath, verifyIL, decoratorMode, outputOptions, metadata, references, target, bundlerMode, absolutePath, lexer.Pragmas, externalRefs);
}
// Package if requested
if (packOptions.Pack)
{
CreateNuGetPackage(outputPath, packageJson, packOptions);
}
}
catch (SharpTSException ex)
{
var reporter = new DiagnosticReporter { MsBuildFormat = outputOptions.MsBuildErrors };
reporter.Report(ex.Diagnostic);
Environment.Exit(1);
}
catch (Exception ex)
{
if (outputOptions.MsBuildErrors)
{
// MSBuild error format: file(line,col): error CODE: message
Console.Error.WriteLine($"{inputPath}(1,1): error SHARPTS000: {ex.Message}");
}
else
{
Console.WriteLine($"Error: {ex.Message}");
}
Environment.Exit(1);
}
}
static void CompileModuleFile(string absolutePath, string outputPath, bool preserveConstEnums, bool useReferenceAssemblies, string? sdkPath, bool verifyIL, DecoratorMode decoratorMode, OutputOptions outputOptions, AssemblyMetadata? metadata, IReadOnlyList<string> references, OutputTarget target, BundlerMode bundlerMode, ReferenceSet externalRefs)
{
// Phase 1: Load all static dependencies via ModuleResolver
var resolver = new ModuleResolver(absolutePath);
var entryModule = resolver.LoadModule(absolutePath, decoratorMode);
var allModules = resolver.GetModulesInOrder(entryModule);
// Phase 2: Initial type checking to discover dynamic import paths
var checker = new TypeChecker();
checker.SetDecoratorMode(decoratorMode);
var typeMap = checker.CheckModules(allModules, resolver);
// Phase 3: Load modules discovered through dynamic import string literals
// These modules aren't in the static dependency graph but need to be compiled
// for runtime dynamic imports to work
var dynamicPaths = checker.DynamicImportPaths;
if (dynamicPaths.Count > 0)
{
var newModules = resolver.LoadDynamicImportModules(dynamicPaths, absolutePath, decoratorMode);
if (newModules.Count > 0)
{
// Re-get the module list to include newly discovered modules
allModules = resolver.GetModulesInOrder(entryModule);
// Re-run type checking with the expanded module list
// (CheckModules is incremental - only checks newly added modules)
typeMap = checker.CheckModules(allModules, resolver);
}
}
// Dead Code Analysis
DeadCodeAnalyzer deadCodeAnalyzer = new(typeMap);
var allStatements = allModules.SelectMany(m => m.Statements).ToList();
DeadCodeInfo deadCodeInfo = deadCodeAnalyzer.Analyze(allStatements);
// Compilation
string assemblyName = Path.GetFileNameWithoutExtension(outputPath);
if (target == OutputTarget.Exe)
{
// For EXE output, first compile to a temp DLL, then bundle into single-file EXE
var tempDllPath = Path.Combine(Path.GetTempPath(), $"{assemblyName}_{Guid.NewGuid():N}.dll");
try
{
// Compile to DLL format (will be bundled into EXE)
ILCompiler compiler = new(assemblyName, preserveConstEnums, useReferenceAssemblies, sdkPath, metadata, references, OutputTarget.Dll);
compiler.SetDecoratorMode(decoratorMode);
compiler.CompileModules(allModules, resolver, typeMap, deadCodeInfo);
compiler.Save(tempDllPath);
// Run IL verification on the DLL if requested
if (verifyIL)
{
VerifyCompiledAssembly(tempDllPath, sdkPath, externalRefs);
}
// Bundle into single-file EXE
try
{
var bundleResult = AppHostGenerator.CreateSingleFileExecutable(tempDllPath, outputPath, assemblyName, bundlerMode);
if (!outputOptions.QuietMode)
{
Console.WriteLine($"Compiled to {outputPath} (using {bundleResult.TechniqueDescription})");
}
// Co-locate SharpTS.dll next to the EXE when the program uses a feature that
// late-binds into the SharpTS runtime (eval, Proxy, Intl, vm, dns, @DotNetType
// dynamic events). Honors --standalone. Pure programs stay a single file.
CopySharpTSRuntimeIfNeeded(compiler, outputPath, outputOptions);
CopyExternalReferencesIfNeeded(compiler, externalRefs, outputPath, outputOptions);
}
catch (Exception ex) when (bundlerMode != BundlerMode.Auto)
{
var bundlerName = bundlerMode == BundlerMode.Sdk ? "SDK" : "built-in";
Console.WriteLine($"Error: {bundlerName} bundler failed: {ex.Message}");
Console.WriteLine($"The {bundlerName} bundler was explicitly requested. Use '--bundler auto' to allow fallback.");
Environment.Exit(1);
}
}
finally
{
// Clean up temp DLL
try { File.Delete(tempDllPath); } catch { }
}
}
else
{
// Standard DLL output
ILCompiler compiler = new(assemblyName, preserveConstEnums, useReferenceAssemblies, sdkPath, metadata, references, target);
compiler.SetDecoratorMode(decoratorMode);
compiler.CompileModules(allModules, resolver, typeMap, deadCodeInfo);
compiler.Save(outputPath);
GenerateRuntimeConfig(outputPath);
CopySharpTSRuntimeIfNeeded(compiler, outputPath, outputOptions);
CopyExternalReferencesIfNeeded(compiler, externalRefs, outputPath, outputOptions);
if (!outputOptions.QuietMode)
{
Console.WriteLine($"Compiled to {outputPath}");
}
// Run IL verification if requested
if (verifyIL)
{
VerifyCompiledAssembly(outputPath, sdkPath, externalRefs);
}
}
}
static void CompileSingleFile(List<Stmt> statements, string outputPath, bool preserveConstEnums, bool useReferenceAssemblies, string? sdkPath, bool verifyIL, DecoratorMode decoratorMode, OutputOptions outputOptions, AssemblyMetadata? metadata, IReadOnlyList<string> references, OutputTarget target, BundlerMode bundlerMode, string? sourcePath = null, TypeScriptPragmas? pragmas = null, ReferenceSet? externalRefs = null)
{
externalRefs ??= ReferenceSet.Empty;
// Set up diagnostic reporter
var reporter = new DiagnosticReporter { MsBuildFormat = outputOptions.MsBuildErrors, QuietMode = outputOptions.QuietMode };
// Static Analysis Phase — skipped for .js files unless `// @ts-check` opts in.
// Compiler still needs a TypeMap; an empty one falls back to dynamic dispatch.
TypeMap typeMap = new();
var effectivePragmas = pragmas ?? TypeScriptPragmas.Empty;
if (TypeCheckPolicy.ShouldTypeCheck(sourcePath, effectivePragmas, checkJsDefault: false))
{
TypeChecker checker = new TypeChecker().WithFilePath(outputPath);
checker.SetDecoratorMode(decoratorMode);
var typeResult = checker.CheckWithRecovery(statements);
// Apply // @ts-ignore / @ts-expect-error line directives.
var filteredDiagnostics = TypeCheckPolicy.ApplyLineDirectives(typeResult.Diagnostics, effectivePragmas);
bool hasErrors = filteredDiagnostics.Any(d => d.Severity == DiagnosticSeverity.Error);
if (hasErrors)
{
reporter.ReportAll(filteredDiagnostics);
if (typeResult.HitErrorLimit)
Console.WriteLine("Too many errors, stopping.");
Environment.Exit(1);
}
typeMap = typeResult.TypeMap;
}
// Dead Code Analysis Phase
DeadCodeAnalyzer deadCodeAnalyzer = new(typeMap);
DeadCodeInfo deadCodeInfo = deadCodeAnalyzer.Analyze(statements);
// Compilation Phase
string assemblyName = Path.GetFileNameWithoutExtension(outputPath);
if (target == OutputTarget.Exe)
{
// For EXE output, first compile to a temp DLL, then bundle into single-file EXE
var tempDllPath = Path.Combine(Path.GetTempPath(), $"{assemblyName}_{Guid.NewGuid():N}.dll");
try
{
// Compile to DLL format (will be bundled into EXE)
ILCompiler compiler = new(assemblyName, preserveConstEnums, useReferenceAssemblies, sdkPath, metadata, references, OutputTarget.Dll);
compiler.SetDecoratorMode(decoratorMode);
compiler.Compile(statements, typeMap, deadCodeInfo);
compiler.Save(tempDllPath);
// Run IL verification on the DLL if requested
if (verifyIL)
{
VerifyCompiledAssembly(tempDllPath, sdkPath, externalRefs);
}
// Bundle into single-file EXE
try
{
var bundleResult = AppHostGenerator.CreateSingleFileExecutable(tempDllPath, outputPath, assemblyName, bundlerMode);
if (!outputOptions.QuietMode)
{
Console.WriteLine($"Compiled to {outputPath} (using {bundleResult.TechniqueDescription})");
}
// Co-locate SharpTS.dll next to the EXE when the program uses a feature that
// late-binds into the SharpTS runtime (eval, Proxy, Intl, vm, dns, @DotNetType
// dynamic events). Honors --standalone. Pure programs stay a single file.
CopySharpTSRuntimeIfNeeded(compiler, outputPath, outputOptions);
CopyExternalReferencesIfNeeded(compiler, externalRefs, outputPath, outputOptions);
}
catch (Exception ex) when (bundlerMode != BundlerMode.Auto)
{
var bundlerName = bundlerMode == BundlerMode.Sdk ? "SDK" : "built-in";
Console.WriteLine($"Error: {bundlerName} bundler failed: {ex.Message}");
Console.WriteLine($"The {bundlerName} bundler was explicitly requested. Use '--bundler auto' to allow fallback.");
Environment.Exit(1);
}
}
finally
{
// Clean up temp DLL
try { File.Delete(tempDllPath); } catch { }
}
}
else
{
// Standard DLL output
ILCompiler compiler = new(assemblyName, preserveConstEnums, useReferenceAssemblies, sdkPath, metadata, references, target);
compiler.SetDecoratorMode(decoratorMode);
compiler.Compile(statements, typeMap, deadCodeInfo);
compiler.Save(outputPath);
GenerateRuntimeConfig(outputPath);
CopySharpTSRuntimeIfNeeded(compiler, outputPath, outputOptions);
CopyExternalReferencesIfNeeded(compiler, externalRefs, outputPath, outputOptions);
if (!outputOptions.QuietMode)
{
Console.WriteLine($"Compiled to {outputPath}");
}
// Run IL verification if requested
if (verifyIL)
{
VerifyCompiledAssembly(outputPath, sdkPath, externalRefs);
}
}
}
/// <summary>
/// Co-locates SharpTS.dll with the compiled output when, and only when, the compilation emitted
/// late binding into the SharpTS runtime whose normal execution needs it (eval, Proxy, Intl, vm,
/// dns, @DotNetType dynamic events). Programs that use none of these stay fully standalone — no
/// copy. <c>--standalone</c> suppresses the copy (the soft-dependent features then throw a clear
/// "not supported" error at runtime instead).
/// </summary>
static void CopySharpTSRuntimeIfNeeded(ILCompiler compiler, string outputPath, OutputOptions outputOptions)
{
var reasons = compiler.RequiredSharpTSRuntimeReasons;
if (reasons.Count == 0)
return; // fully standalone — nothing to co-locate
string reasonList = string.Join(", ", reasons);
if (outputOptions.Standalone)
{
if (!outputOptions.QuietMode)
Console.WriteLine(
$"Note: output uses features needing the SharpTS runtime ({reasonList}); " +
"--standalone set, so SharpTS.dll was not copied. These features will throw at runtime unless SharpTS.dll is present.");
return;
}
var sharpTsPath = typeof(SharpTS.Execution.Interpreter).Assembly.Location;
var outDir = Path.GetDirectoryName(Path.GetFullPath(outputPath));
if (string.IsNullOrEmpty(sharpTsPath) || !File.Exists(sharpTsPath) || outDir == null)
{
if (!outputOptions.QuietMode)
Console.WriteLine($"Warning: could not locate SharpTS.dll to co-locate with output; features ({reasonList}) may fail at runtime.");
return;
}
var destPath = Path.Combine(outDir, Path.GetFileName(sharpTsPath));
try
{
if (!string.Equals(Path.GetFullPath(sharpTsPath), Path.GetFullPath(destPath), StringComparison.OrdinalIgnoreCase))
File.Copy(sharpTsPath, destPath, overwrite: true);
// child_process.fork() spawns a SEPARATE `dotnet exec SharpTS.dll <module>` process
// (unlike Worker/eval which load SharpTS.dll in-process). That child needs SharpTS's
// full runtime closure — its runtimeconfig.json, deps.json, and dependency DLLs — so
// co-locate the whole SharpTS bin directory next to the output. Other soft-deps only
// need SharpTS.dll loaded in-process.
if (reasons.Contains("child_process.fork"))
{
var sharpTsDir = Path.GetDirectoryName(sharpTsPath);
if (!string.IsNullOrEmpty(sharpTsDir))
{
foreach (var src in Directory.EnumerateFiles(sharpTsDir))
{
var name = Path.GetFileName(src);
var ext = Path.GetExtension(name);
bool isRuntimeFile = ext.Equals(".dll", StringComparison.OrdinalIgnoreCase)
|| name.Equals("SharpTS.runtimeconfig.json", StringComparison.OrdinalIgnoreCase)
|| name.Equals("SharpTS.deps.json", StringComparison.OrdinalIgnoreCase);
if (!isRuntimeFile) continue;
var dst = Path.Combine(outDir, name);
if (string.Equals(Path.GetFullPath(src), Path.GetFullPath(dst), StringComparison.OrdinalIgnoreCase))
continue;
File.Copy(src, dst, overwrite: true);
}
}
}
if (!outputOptions.QuietMode)
{
var what = reasons.Contains("child_process.fork") ? "SharpTS runtime" : "SharpTS.dll";
Console.WriteLine($"Copied {what} next to output — required at runtime by: {reasonList}");
}
}
catch (Exception ex)
{
if (!outputOptions.QuietMode)
Console.WriteLine($"Warning: failed to co-locate SharpTS.dll with output ({reasonList}): {ex.Message}");
}
}
/// <summary>
/// Co-locates third-party reference assemblies (sharpts.json / -r) with the compiled output.
/// Unlike the SharpTS runtime soft-dependency, these are HARD metadata references — the
/// emitted IL calls into them by token, and default host probing only searches the app
/// directory. Copies only the assemblies whose types the program actually used, plus their
/// transitive dependency closure (assets-graph subtree for NuGet packages, AssemblyName walk
/// for local DLLs). <c>--standalone</c> suppresses the copy but lists what deployment needs.
/// </summary>
static void CopyExternalReferencesIfNeeded(ILCompiler compiler, ReferenceSet externalRefs, string outputPath, OutputOptions outputOptions)
{
if (externalRefs.IsEmpty)
return;
// Which reference DLLs did the compilation actually bind types from?
var used = new List<ResolvedReference>();
foreach (var assembly in compiler.ExternalInteropAssemblies)
{
string location;
try { location = assembly.Location; } catch { continue; }
if (string.IsNullOrEmpty(location)) continue;
var match = externalRefs.FindByPath(Path.GetFullPath(location));
if (match != null && !used.Contains(match)) used.Add(match);
}
if (used.Count == 0)
return; // external types came only from the BCL / already-deployed assemblies
// Loaded assemblies by full path, for the local-DLL dependency walk.
var loadedByPath = new Dictionary<string, Assembly>(
OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
if (!assembly.IsDynamic && !string.IsNullOrEmpty(assembly.Location))
loadedByPath[Path.GetFullPath(assembly.Location)] = assembly;
}
catch { }
}
var copySet = new List<string>();
var seen = new HashSet<string>(loadedByPath.Comparer);
void AddLocalClosure(string dllPath)
{
if (!seen.Add(dllPath)) return;
copySet.Add(dllPath);
// Dependencies resolve from the reference set (recurse) or as siblings of the
// DLL that references them (copy). Shared-framework names match neither and
// fall out naturally.
if (!loadedByPath.TryGetValue(dllPath, out var assembly)) return;
foreach (var dependency in assembly.GetReferencedAssemblies())
{
string fileName = dependency.Name + ".dll";
var inSet = externalRefs.References.FirstOrDefault(r =>
string.Equals(Path.GetFileName(r.Path), fileName, StringComparison.OrdinalIgnoreCase));
if (inSet != null)
{
AddLocalClosure(inSet.Path);
continue;
}
string sibling = Path.Combine(Path.GetDirectoryName(dllPath)!, fileName);
if (File.Exists(sibling) && seen.Add(sibling))
copySet.Add(sibling);
}
}
foreach (var reference in used)
{
if (reference.Origin == ReferenceOrigin.Package)
{
foreach (var asset in externalRefs.RuntimeClosureFor(reference))
{
if (seen.Add(asset)) copySet.Add(asset);
}
// The closure always contains the package's own assets, but keep the used
// DLL itself even if the closure lookup came up empty.
if (seen.Add(reference.Path)) copySet.Add(reference.Path);
}
else
{
AddLocalClosure(reference.Path);
}
}
string usedNames = string.Join(", ", used.Select(r => Path.GetFileNameWithoutExtension(r.Path)));
if (outputOptions.Standalone)
{
if (!outputOptions.QuietMode)
Console.WriteLine(
$"Note: output references external .NET assemblies ({usedNames}); --standalone set, so they were " +
"not copied. The output will not run unless these assemblies are deployed next to it: " +
string.Join(", ", copySet.Select(Path.GetFileName)));
return;
}
var outDir = Path.GetDirectoryName(Path.GetFullPath(outputPath))!;
int copied = 0;
foreach (var source in copySet)
{
var destPath = Path.Combine(outDir, Path.GetFileName(source));
try
{
if (!string.Equals(Path.GetFullPath(source), Path.GetFullPath(destPath), StringComparison.OrdinalIgnoreCase))
{
File.Copy(source, destPath, overwrite: true);
copied++;
}
}
catch (Exception ex)
{
if (!outputOptions.QuietMode)
Console.WriteLine($"Warning: failed to copy referenced assembly '{source}' next to output: {ex.Message}");
}
}
if (copied > 0 && !outputOptions.QuietMode)
{
Console.WriteLine($"Copied {copied} referenced assembl{(copied == 1 ? "y" : "ies")} next to output — required by external .NET types: {usedNames}");
}
}
static void GenerateRuntimeConfig(string outputPath)
{
string runtimeConfigPath = Path.ChangeExtension(outputPath, ".runtimeconfig.json");
string runtimeConfig = """
{
"runtimeOptions": {
"tfm": "net10.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "10.0.0"
}
}
}
""";
File.WriteAllText(runtimeConfigPath, runtimeConfig);
}
static void VerifyCompiledAssembly(string outputPath, string? sdkPath, ReferenceSet? externalRefs = null)
{
// The verifier resolves against the shared-framework runtime directory; an explicit
// --sdk-path and the directories of third-party reference assemblies (whose types the
// emitted IL references by token) are additional probe locations.
var probeDirs = externalRefs?.References
.Select(r => Path.GetDirectoryName(r.Path))
.Where(d => !string.IsNullOrEmpty(d))
.Cast<string>();
using var verifier = new ILVerifier(sdkPath, probeDirs);
using var stream = File.OpenRead(outputPath);
verifier.VerifyAndReport(stream);
}
static void CreateNuGetPackage(string assemblyPath, PackageJson? packageJson, PackOptions packOptions)
{
// Create a minimal package.json if one wasn't found but we have CLI overrides
packageJson ??= new PackageJson
{
Name = packOptions.PackageIdOverride,
Version = packOptions.VersionOverride ?? "1.0.0"
};
// Validate the package configuration
var validation = PackageValidator.Validate(
assemblyPath,
packageJson,
packOptions.PackageIdOverride,
packOptions.VersionOverride);
// Print warnings
foreach (var warning in validation.Warnings)
{
Console.WriteLine($"Warning: {warning}");
}
// Check for errors
if (!validation.IsValid)
{
foreach (var error in validation.Errors)
{
Console.WriteLine($"Error: {error}");
}
Environment.Exit(1);
}
// Create the NuGet packager
var packager = new NuGetPackager(packageJson, packOptions.PackageIdOverride, packOptions.VersionOverride);
var outputDir = Path.GetDirectoryName(assemblyPath) ?? ".";
// Look for README.md in the package.json directory
string? readmePath = null;
var candidateReadme = Path.Combine(outputDir, "README.md");
if (File.Exists(candidateReadme))
{
readmePath = candidateReadme;
}
// Create the main package
var nupkgPath = packager.CreatePackage(assemblyPath, outputDir, readmePath);
Console.WriteLine($"Created package: {nupkgPath}");
// Create symbol package
var symbolPackager = new SymbolPackager(packager.PackageId, packager.Version, packageJson.Author);
var snupkgPath = symbolPackager.CreateSymbolPackage(assemblyPath, outputDir);
if (snupkgPath != null)
{
Console.WriteLine($"Created symbol package: {snupkgPath}");
}
// Push to NuGet feed if requested
if (!string.IsNullOrEmpty(packOptions.PushSource))
{
if (string.IsNullOrEmpty(packOptions.ApiKey))
{
Console.WriteLine("Error: --api-key is required when using --push.");
Environment.Exit(1);
}
Console.WriteLine($"Pushing to {packOptions.PushSource}...");
var publisher = new NuGetPublisher(packOptions.ApiKey, packOptions.PushSource);
var success = publisher.PushWithSymbolsAsync(nupkgPath, snupkgPath).GetAwaiter().GetResult();
if (success)
{
Console.WriteLine($"Successfully pushed {packager.PackageId} {packager.Version}");
}
else
{
Console.WriteLine("Push failed.");
Environment.Exit(1);
}
}
}
static void GenerateDeclarations(string typeOrAssembly, string? outputPath, bool json, IReadOnlyList<string> references)
{
// Manifest (from CWD) + -r assemblies load first so type/namespace discovery
// sees third-party types, not just already-loaded ones.
LoadDotNetReferences(Environment.CurrentDirectory, references);
try
{
// --gen-decl is a .NET interop *discovery* tool (issue #1194): it inspects a type,
// namespace, or assembly and reports which members are usable from TypeScript interop,
// with the `dotnet:` import line for usable types. It no longer emits pasteable TS
// source (which was lossy for Span<T>/ref/pointer surfaces — see #1193).
var generator = new DiscoveryGenerator();