diff --git a/pkg/dartdev/lib/src/commands/compile.dart b/pkg/dartdev/lib/src/commands/compile.dart index 600c1c257d1..bf5b15e992d 100644 --- a/pkg/dartdev/lib/src/commands/compile.dart +++ b/pkg/dartdev/lib/src/commands/compile.dart @@ -709,17 +709,26 @@ Remove debugging information from the output and save it separately to the speci verbose: verbose, target: target, ); - if (!nativeAssetsExperimentEnabled) { - if (await builder.warnOnNativeAssets()) { + + final isBinScript = path.isWithin( + path.canonicalize(path.join(Directory.current.path, 'bin')), + path.canonicalize(sourcePath), + ); + if (isBinScript) { + if (!nativeAssetsExperimentEnabled) { + if (await builder.warnOnNativeAssets()) { + return 255; + } + } else if (await builder.hasHooks()) { + final packages = (await builder.packagesWithBuildHooks()).join( + ', ', + ); + stderr.writeln( + "'dart compile' does not support build hooks, use 'dart build' instead.\n" + 'Packages with build hooks: $packages.', + ); return 255; } - } else if (await builder.hasHooks()) { - final packages = (await builder.packagesWithBuildHooks()).join(', '); - stderr.writeln( - "'dart compile' does not support build hooks, use 'dart build' instead.\n" - 'Packages with build hooks: $packages.', - ); - return 255; } } } diff --git a/pkg/dartdev/test/native_assets/compile_test.dart b/pkg/dartdev/test/native_assets/compile_test.dart index b46c40649d9..31170087f88 100644 --- a/pkg/dartdev/test/native_assets/compile_test.dart +++ b/pkg/dartdev/test/native_assets/compile_test.dart @@ -49,6 +49,58 @@ void main() async { }); }); + test('dart compile only bails in bin/', timeout: longTimeout, () async { + await nativeAssetsTest('dart_app', (dartAppUri) async { + await runDart( + arguments: ['pub', 'get'], + workingDirectory: dartAppUri, + logger: logger, + ); + + // 1. Compiling in bin/ should fail. + final resultBin = await runDart( + arguments: [ + 'compile', + 'exe', + 'bin/dart_app.dart', + ], + workingDirectory: dartAppUri, + logger: logger, + expectExitCodeZero: false, + ); + expect(resultBin.exitCode, 255); + expect( + resultBin.stderr, + contains( + "'dart compile' does not support build hooks, use 'dart build' instead.", + ), + ); + + // 2. Compiling outside bin/ should succeed (even if it might fail at runtime). + final otherFile = File.fromUri(dartAppUri.resolve('tool/other.dart')); + await otherFile.parent.create(recursive: true); + await otherFile.writeAsString('void main() { print("hello"); }'); + + final resultOther = await runDart( + arguments: [ + '-v', + 'compile', + 'exe', + 'tool/other.dart', + ], + workingDirectory: dartAppUri, + logger: logger, + ); + expect(resultOther.exitCode, 0); + final exePath = dartAppUri.resolve('tool/other.exe').toFilePath(); + final resultRun = await runProcess( + executable: Uri.file(exePath), + logger: logger, + ); + expect(resultRun.stdout, contains('hello')); + }); + }); + test('Recorded usages in dart2js', timeout: longTimeout, () async { await recordUseTest('drop_data_asset', (dartAppUri) async { await runDart(