diff --git a/pkg/vm/bin/kernel_service.dart b/pkg/vm/bin/kernel_service.dart index 021c8583011..abd87c32e25 100644 --- a/pkg/vm/bin/kernel_service.dart +++ b/pkg/vm/bin/kernel_service.dart @@ -100,7 +100,8 @@ CompilerOptions setupCompilerOptions( int nullSafety, List experimentalFlags, bool bytecode, - Uri packagesUri, + String packageConfig, + String workingDirectory, List errors) { final expFlags = []; if (experimentalFlags != null) { @@ -108,7 +109,17 @@ CompilerOptions setupCompilerOptions( expFlags.addAll(flag.split(",")); } } - + Uri packagesUri = null; + if (packageConfig != null) { + packagesUri = Uri.parse(packageConfig); + if (packagesUri.scheme == '') { + // Script does not have a scheme, assume that it is a path, + // resolve it against the working directory. + packagesUri = Uri.directory(workingDirectory).resolveUri(packagesUri); + } + } else if (Platform.packageConfig != null) { + packagesUri = Uri.parse(Platform.packageConfig); + } return new CompilerOptions() ..fileSystem = fileSystem ..target = new VmTarget(new TargetFlags( @@ -158,7 +169,7 @@ abstract class Compiler { final List experimentalFlags; final bool bytecode; final String packageConfig; - + final String workingDirectory; // Code coverage and hot reload are only supported by incremental compiler, // which is used if vm-service is enabled. final bool supportCodeCoverage; @@ -176,21 +187,8 @@ abstract class Compiler { this.bytecode: false, this.supportCodeCoverage: false, this.supportHotReload: false, - this.packageConfig: null}) { - Uri packagesUri = null; - if (packageConfig != null) { - packagesUri = Uri.parse(packageConfig); - } else if (Platform.packageConfig != null) { - packagesUri = Uri.parse(Platform.packageConfig); - } - - if (verbose) { - print("DFE: Platform.packageConfig: ${Platform.packageConfig}"); - print("DFE: packagesUri: ${packagesUri}"); - print("DFE: Platform.resolvedExecutable: ${Platform.resolvedExecutable}"); - print("DFE: platformKernelPath: ${platformKernelPath}"); - } - + this.packageConfig: null, + this.workingDirectory: null}) { options = setupCompilerOptions( fileSystem, platformKernelPath, @@ -199,8 +197,16 @@ abstract class Compiler { nullSafety, experimentalFlags, bytecode, - packagesUri, + packageConfig, + workingDirectory, errors); + + if (verbose) { + print("DFE: Platform.packageConfig: ${Platform.packageConfig}"); + print("DFE: packagesUri: ${options.packagesFileUri}"); + print("DFE: Platform.resolvedExecutable: ${Platform.resolvedExecutable}"); + print("DFE: platformKernelPath: ${platformKernelPath}"); + } } Future compile(Uri script) { @@ -337,7 +343,8 @@ class IncrementalCompilerWrapper extends Compiler { int nullSafety: kNullSafetyOptionUnspecified, List experimentalFlags: null, bool bytecode: false, - String packageConfig: null}) + String packageConfig: null, + String workingDirectory: null}) : super(isolateId, fileSystem, platformKernelPath, suppressWarnings: suppressWarnings, enableAsserts: enableAsserts, @@ -346,7 +353,8 @@ class IncrementalCompilerWrapper extends Compiler { bytecode: bytecode, supportHotReload: true, supportCodeCoverage: true, - packageConfig: packageConfig); + packageConfig: packageConfig, + workingDirectory: workingDirectory); factory IncrementalCompilerWrapper.forExpressionCompilationOnly( Component component, @@ -357,7 +365,8 @@ class IncrementalCompilerWrapper extends Compiler { bool enableAsserts: false, List experimentalFlags: null, bool bytecode: false, - String packageConfig: null}) { + String packageConfig: null, + String workingDirectory: null}) { IncrementalCompilerWrapper result = IncrementalCompilerWrapper( isolateId, fileSystem, platformKernelPath, suppressWarnings: suppressWarnings, @@ -393,7 +402,8 @@ class IncrementalCompilerWrapper extends Compiler { nullSafety: nullSafety, experimentalFlags: experimentalFlags, bytecode: bytecode, - packageConfig: packageConfig); + packageConfig: packageConfig, + workingDirectory: workingDirectory); generator.resetDeltaState(); Component fullComponent = await generator.compile(); @@ -424,14 +434,16 @@ class SingleShotCompilerWrapper extends Compiler { int nullSafety: kNullSafetyOptionUnspecified, List experimentalFlags: null, bool bytecode: false, - String packageConfig: null}) + String packageConfig: null, + String workingDirectory: null}) : super(isolateId, fileSystem, platformKernelPath, suppressWarnings: suppressWarnings, enableAsserts: enableAsserts, nullSafety: nullSafety, experimentalFlags: experimentalFlags, bytecode: bytecode, - packageConfig: packageConfig); + packageConfig: packageConfig, + workingDirectory: workingDirectory); @override Future compileInternal(Uri script) async { @@ -467,6 +479,7 @@ Future lookupOrBuildNewIncrementalCompiler(int isolateId, List experimentalFlags: null, bool bytecode: false, String packageConfig: null, + String workingDirectory: null, String multirootFilepaths, String multirootScheme}) async { IncrementalCompilerWrapper compiler = lookupIncrementalCompiler(isolateId); @@ -480,7 +493,8 @@ Future lookupOrBuildNewIncrementalCompiler(int isolateId, if (sourceFiles != null && sourceFiles.length > 0 && sourceFiles[1] == null) { - // Just use first compiler that should represent main isolate as a source for cloning. + // Just use first compiler that should represent main isolate as a source + // for cloning. var source = isolateCompilers.entries.first; compiler = await source.value.clone(isolateId); } else { @@ -498,7 +512,8 @@ Future lookupOrBuildNewIncrementalCompiler(int isolateId, nullSafety: nullSafety, experimentalFlags: experimentalFlags, bytecode: bytecode, - packageConfig: packageConfig); + packageConfig: packageConfig, + workingDirectory: workingDirectory); } isolateCompilers[isolateId] = compiler; } @@ -822,20 +837,18 @@ Future _processLoadRequest(request) async { } else if (tag == kDetectNullabilityTag) { FileSystem fileSystem = _buildFileSystem( sourceFiles, platformKernel, multirootFilepaths, multirootScheme); - Uri packagesUri = null; - if (packageConfig != null) { - packagesUri = Uri.parse(packageConfig); - } else if (Platform.packageConfig != null) { - packagesUri = Uri.parse(Platform.packageConfig); - } - if (packagesUri != null && packagesUri.scheme == '') { - // Script does not have a scheme, assume that it is a path, - // resolve it against the working directory. - packagesUri = Uri.directory(workingDirectory).resolveUri(packagesUri); - } final List errors = []; - var options = setupCompilerOptions(fileSystem, platformKernelPath, false, - false, nullSafety, experimentalFlags, false, packagesUri, errors); + var options = setupCompilerOptions( + fileSystem, + platformKernelPath, + false, + false, + nullSafety, + experimentalFlags, + false, + packageConfig, + workingDirectory, + errors); // script should only be null for kUpdateSourcesTag. assert(script != null); @@ -861,6 +874,7 @@ Future _processLoadRequest(request) async { experimentalFlags: experimentalFlags, bytecode: bytecode, packageConfig: packageConfig, + workingDirectory: workingDirectory, multirootFilepaths: multirootFilepaths, multirootScheme: multirootScheme); } else { @@ -874,7 +888,8 @@ Future _processLoadRequest(request) async { nullSafety: nullSafety, experimentalFlags: experimentalFlags, bytecode: bytecode, - packageConfig: packageConfig); + packageConfig: packageConfig, + workingDirectory: workingDirectory); } CompilationResult result; diff --git a/runtime/bin/dartutils.cc b/runtime/bin/dartutils.cc index f1eeeb2a946..f8f374d2b03 100644 --- a/runtime/bin/dartutils.cc +++ b/runtime/bin/dartutils.cc @@ -302,15 +302,6 @@ bool DartUtils::EntropySource(uint8_t* buffer, intptr_t length) { return Crypto::GetRandomBytes(length, buffer); } -static Dart_Handle SingleArgDart_Invoke(Dart_Handle lib, - const char* method, - Dart_Handle arg) { - const int kNumArgs = 1; - Dart_Handle dart_args[kNumArgs]; - dart_args[0] = arg; - return Dart_Invoke(lib, DartUtils::NewString(method), kNumArgs, dart_args); -} - // TODO(iposva): Allocate from the zone instead of leaking error string // here. On the other hand the binary is about to exit anyway. #define SET_ERROR_MSG(error_msg, format, ...) \ @@ -370,20 +361,6 @@ Dart_Handle DartUtils::MakeUint8Array(const uint8_t* buffer, intptr_t len) { return array; } -Dart_Handle DartUtils::SetWorkingDirectory() { - Dart_Handle directory = NewString(original_working_directory); - return SingleArgDart_Invoke(LookupBuiltinLib(), "_setWorkingDirectory", - directory); -} - -Dart_Handle DartUtils::ResolveScript(Dart_Handle url) { - const int kNumArgs = 1; - Dart_Handle dart_args[kNumArgs]; - dart_args[0] = url; - return Dart_Invoke(DartUtils::LookupBuiltinLib(), - NewString("_resolveScriptUri"), kNumArgs, dart_args); -} - static bool CheckMagicNumber(const uint8_t* buffer, intptr_t buffer_length, const MagicNumberData& magic_number) { @@ -465,9 +442,6 @@ Dart_Handle DartUtils::PrepareBuiltinLibrary(Dart_Handle builtin_lib, Dart_SetField(builtin_lib, NewString("_traceLoading"), Dart_True()); RETURN_IF_ERROR(result); } - // Set current working directory. - result = SetWorkingDirectory(); - RETURN_IF_ERROR(result); } return Dart_True(); } @@ -514,21 +488,6 @@ Dart_Handle DartUtils::PrepareCLILibrary(Dart_Handle cli_lib) { wait_for_event_handle); } -Dart_Handle DartUtils::SetupPackageConfig(const char* packages_config) { - Dart_Handle result = Dart_Null(); - - if (packages_config != NULL) { - result = NewString(packages_config); - RETURN_IF_ERROR(result); - const int kNumArgs = 1; - Dart_Handle dart_args[kNumArgs]; - dart_args[0] = result; - result = Dart_Invoke(DartUtils::LookupBuiltinLib(), - NewString("_setPackagesMap"), kNumArgs, dart_args); - } - return result; -} - Dart_Handle DartUtils::PrepareForScriptLoading(bool is_service_isolate, bool trace_loading) { // First ensure all required libraries are available. diff --git a/runtime/bin/dartutils.h b/runtime/bin/dartutils.h index 23122adf2cb..c2fa094d783 100644 --- a/runtime/bin/dartutils.h +++ b/runtime/bin/dartutils.h @@ -161,8 +161,6 @@ class DartUtils { static Dart_Handle MakeUint8Array(const uint8_t* buffer, intptr_t length); static Dart_Handle PrepareForScriptLoading(bool is_service_isolate, bool trace_loading); - static Dart_Handle SetupPackageConfig(const char* packages_file); - static Dart_Handle SetupIOLibrary(const char* namespc_path, const char* script_uri, bool disable_exit); @@ -225,8 +223,6 @@ class DartUtils { static bool SetOriginalWorkingDirectory(); - static Dart_Handle ResolveScript(Dart_Handle url); - enum MagicNumber { kAppJITMagicNumber, kKernelMagicNumber, @@ -265,7 +261,6 @@ class DartUtils { static Dart_Handle EnvironmentCallback(Dart_Handle name); private: - static Dart_Handle SetWorkingDirectory(); static Dart_Handle PrepareBuiltinLibrary(Dart_Handle builtin_lib, Dart_Handle internal_lib, bool is_service_isolate, diff --git a/runtime/bin/dfe.cc b/runtime/bin/dfe.cc index 45b625d9ac6..36e39a5ed65 100644 --- a/runtime/bin/dfe.cc +++ b/runtime/bin/dfe.cc @@ -240,7 +240,8 @@ Dart_KernelCompilationResult DFE::CompileScript(const char* script_uri, return Dart_CompileToKernel( sanitized_uri, platform_strong_dill_for_compilation_, - platform_strong_dill_for_compilation_size_, incremental, package_config); + platform_strong_dill_for_compilation_size_, incremental, package_config, + DartUtils::original_working_directory); } void DFE::CompileAndReadScript(const char* script_uri, diff --git a/runtime/bin/isolate_data.cc b/runtime/bin/isolate_data.cc index 154797507aa..4fca8c41315 100644 --- a/runtime/bin/isolate_data.cc +++ b/runtime/bin/isolate_data.cc @@ -15,7 +15,6 @@ IsolateGroupData::IsolateGroupData(const char* url, bool isolate_run_app_snapshot) : script_url((url != NULL) ? strdup(url) : NULL), app_snapshot_(app_snapshot), - resolved_packages_config_(NULL), kernel_buffer_(NULL), kernel_buffer_size_(0), isolate_run_app_snapshot_(isolate_run_app_snapshot) { @@ -29,8 +28,6 @@ IsolateGroupData::~IsolateGroupData() { script_url = NULL; free(packages_file_); packages_file_ = NULL; - free(resolved_packages_config_); - resolved_packages_config_ = NULL; kernel_buffer_ = NULL; kernel_buffer_size_ = 0; } diff --git a/runtime/bin/isolate_data.h b/runtime/bin/isolate_data.h index 6dae87a3842..f21828571e5 100644 --- a/runtime/bin/isolate_data.h +++ b/runtime/bin/isolate_data.h @@ -74,18 +74,6 @@ class IsolateGroupData { kernel_buffer_size_ = size; } - const char* resolved_packages_config() const { - return resolved_packages_config_; - } - - void set_resolved_packages_config(const char* packages_config) { - if (resolved_packages_config_ != NULL) { - free(resolved_packages_config_); - resolved_packages_config_ = NULL; - } - resolved_packages_config_ = strdup(packages_config); - } - bool RunFromAppSnapshot() const { // If the main isolate is using an app snapshot the [app_snapshot_] pointer // will be still nullptr (see main.cc:CreateIsolateGroupAndSetupHelper) @@ -99,7 +87,6 @@ class IsolateGroupData { friend class IsolateData; // For packages_file_ std::unique_ptr app_snapshot_; - char* resolved_packages_config_; std::shared_ptr kernel_buffer_; intptr_t kernel_buffer_size_; char* packages_file_ = nullptr; diff --git a/runtime/bin/loader.cc b/runtime/bin/loader.cc index 6868012ca87..5c8c302ee9f 100644 --- a/runtime/bin/loader.cc +++ b/runtime/bin/loader.cc @@ -23,27 +23,22 @@ namespace bin { extern DFE dfe; #endif -Dart_Handle Loader::InitForSnapshot(const char* snapshot_uri, - IsolateData* isolate_data) { - ASSERT(isolate_data != NULL); - - return Loader::Init(isolate_data->packages_file(), - DartUtils::original_working_directory, snapshot_uri); -} - // Initialize package resolution state. -Dart_Handle Loader::Init(const char* packages_file, - const char* working_directory, - const char* root_script_uri) { +// Returns the resolved script URI after it resolves 'script_uri' in the +// current working directory iff the given uri did not specify a scheme +// (e.g. a path to a script file on the command line). +Dart_Handle Loader::Init(const char* script_uri, IsolateData* isolate_data) { + ASSERT(isolate_data != NULL); + const char* packages_file = isolate_data->packages_file(); const int kNumArgs = 3; Dart_Handle dart_args[kNumArgs]; dart_args[0] = (packages_file == NULL) ? Dart_Null() : Dart_NewStringFromCString(packages_file); - dart_args[1] = Dart_NewStringFromCString(working_directory); - dart_args[2] = (root_script_uri == NULL) - ? Dart_Null() - : Dart_NewStringFromCString(root_script_uri); + dart_args[1] = + Dart_NewStringFromCString(DartUtils::original_working_directory); + dart_args[2] = (script_uri == NULL) ? Dart_Null() + : Dart_NewStringFromCString(script_uri); return Dart_Invoke(DartUtils::LookupBuiltinLib(), DartUtils::NewString("_Init"), kNumArgs, dart_args); } diff --git a/runtime/bin/loader.h b/runtime/bin/loader.h index f4715e91dac..a3460253a3a 100644 --- a/runtime/bin/loader.h +++ b/runtime/bin/loader.h @@ -17,8 +17,7 @@ namespace bin { class Loader { public: - static Dart_Handle InitForSnapshot(const char* snapshot_uri, - IsolateData* isolate_data); + static Dart_Handle Init(const char* script_uri, IsolateData* isolate_data); static Dart_Handle ReloadNativeExtensions(); @@ -30,10 +29,6 @@ class Loader { static void InitOnce(); private: - static Dart_Handle Init(const char* packages_file, - const char* working_directory, - const char* root_script_uri); - static Dart_Handle LoadImportExtension(const char* url_string, Dart_Handle library); diff --git a/runtime/bin/main.cc b/runtime/bin/main.cc index e99d675fe98..4d76a734c5c 100644 --- a/runtime/bin/main.cc +++ b/runtime/bin/main.cc @@ -174,10 +174,8 @@ static void OnExitHook(int64_t exit_code) { static Dart_Handle SetupCoreLibraries(Dart_Isolate isolate, IsolateData* isolate_data, - bool is_isolate_group_start, - const char** resolved_packages_config) { + bool is_isolate_group_start) { auto isolate_group_data = isolate_data->isolate_group_data(); - const auto packages_file = isolate_data->packages_file(); const auto script_uri = isolate_group_data->script_url; Dart_Handle result; @@ -188,24 +186,6 @@ static Dart_Handle SetupCoreLibraries(Dart_Isolate isolate, result = DartUtils::PrepareForScriptLoading(false, Options::trace_loading()); if (Dart_IsError(result)) return result; - // Setup packages config if specified. - result = DartUtils::SetupPackageConfig(packages_file); - if (Dart_IsError(result)) return result; - if (!Dart_IsNull(result) && resolved_packages_config != nullptr) { - result = Dart_StringToCString(result, resolved_packages_config); - if (Dart_IsError(result)) return result; - ASSERT(*resolved_packages_config != nullptr); -#if !defined(DART_PRECOMPILED_RUNTIME) - if (is_isolate_group_start) { - isolate_group_data->set_resolved_packages_config( - *resolved_packages_config); - } else { - ASSERT(strcmp(isolate_group_data->resolved_packages_config(), - *resolved_packages_config) == 0); - } -#endif - } - result = Dart_SetEnvironmentCallback(DartUtils::EnvironmentCallback); if (Dart_IsError(result)) return result; @@ -235,35 +215,17 @@ static bool OnIsolateInitialize(void** child_callback_data, char** error) { *child_callback_data = isolate_data; Dart_EnterScope(); + Dart_Handle resolved_script_uri; const auto script_uri = isolate_group_data->script_url; const bool isolate_run_app_snapshot = isolate_group_data->RunFromAppSnapshot(); Dart_Handle result = SetupCoreLibraries(isolate, isolate_data, - /*group_start=*/false, - /*resolved_packages_config=*/nullptr); + /*group_start=*/false); if (Dart_IsError(result)) goto failed; - if (isolate_run_app_snapshot) { - if (Dart_IsVMFlagSet("support_service") || !Dart_IsPrecompiledRuntime()) { - result = Loader::InitForSnapshot(script_uri, isolate_data); - if (Dart_IsError(result)) goto failed; - } - } else { - result = DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); - if (Dart_IsError(result)) return result != nullptr; - - if (isolate_group_data->kernel_buffer().get() != nullptr) { - // Various core-library parts will send requests to the Loader to resolve - // relative URIs and perform other related tasks. We need Loader to be - // initialized for this to work because loading from Kernel binary - // bypasses normal source code loading paths that initialize it. - const char* resolved_script_uri = NULL; - result = Dart_StringToCString(result, &resolved_script_uri); - if (Dart_IsError(result)) goto failed; - result = Loader::InitForSnapshot(resolved_script_uri, isolate_data); - if (Dart_IsError(result)) goto failed; - } - } + // Resolve script URI and Initialize package resolution state. + resolved_script_uri = Loader::Init(script_uri, isolate_data); + if (Dart_IsError(resolved_script_uri)) goto failed; if (isolate_run_app_snapshot) { result = Loader::ReloadNativeExtensions(); @@ -300,10 +262,8 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, auto isolate_data = reinterpret_cast(Dart_IsolateData(isolate)); - const char* resolved_packages_config = nullptr; result = SetupCoreLibraries(isolate, isolate_data, - /*is_isolate_group_start=*/true, - &resolved_packages_config); + /*is_isolate_group_start=*/true); CHECK_RESULT(result); #if !defined(DART_PRECOMPILED_RUNTIME) @@ -327,7 +287,7 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, intptr_t application_kernel_buffer_size = 0; dfe.CompileAndReadScript(script_uri, &application_kernel_buffer, &application_kernel_buffer_size, error, exit_code, - resolved_packages_config); + packages_config); if (application_kernel_buffer == NULL) { Dart_ExitScope(); Dart_ShutdownIsolate(); @@ -338,11 +298,8 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, kernel_buffer = application_kernel_buffer; kernel_buffer_size = application_kernel_buffer_size; } + // Load the specified application script into the newly created isolate. if (kernel_buffer != NULL) { - Dart_Handle uri = Dart_NewStringFromCString(script_uri); - CHECK_RESULT(uri); - Dart_Handle resolved_script_uri = DartUtils::ResolveScript(uri); - CHECK_RESULT(resolved_script_uri); result = Dart_LoadScriptFromKernel(kernel_buffer, kernel_buffer_size); CHECK_RESULT(result); } @@ -353,47 +310,29 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, CHECK_RESULT(result); } - if (isolate_run_app_snapshot) { - if (Dart_IsVMFlagSet("support_service") || !Dart_IsPrecompiledRuntime()) { - Dart_Handle result = Loader::InitForSnapshot(script_uri, isolate_data); - CHECK_RESULT(result); - } + // Resolve script URI and Initialize package resolution state. + Dart_Handle resolved_script_uri = Loader::Init(script_uri, isolate_data); + CHECK_RESULT(resolved_script_uri); #if !defined(DART_PRECOMPILED_RUNTIME) + if (isolate_run_app_snapshot) { if (is_main_isolate) { // Find the canonical uri of the app snapshot. We'll use this to decide if // other isolates should use the app snapshot or the core snapshot. - const char* resolved_script_uri = NULL; - result = Dart_StringToCString( - DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)), - &resolved_script_uri); + const char* resolved_script = NULL; + result = Dart_StringToCString(resolved_script_uri, &resolved_script); CHECK_RESULT(result); ASSERT(app_script_uri == NULL); - app_script_uri = strdup(resolved_script_uri); + app_script_uri = strdup(resolved_script); } -#endif // !defined(DART_PRECOMPILED_RUNTIME) - } else { -#if !defined(DART_PRECOMPILED_RUNTIME) - // Load the specified application script into the newly created isolate. - Dart_Handle uri = - DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); - CHECK_RESULT(uri); - if (kernel_buffer != NULL) { - // relative URIs and perform other related tasks. We need Loader to be - // initialized for this to work because loading from Kernel binary - // bypasses normal source code loading paths that initialize it. - const char* resolved_script_uri = NULL; - result = Dart_StringToCString(uri, &resolved_script_uri); - CHECK_RESULT(result); - result = Loader::InitForSnapshot(resolved_script_uri, isolate_data); - CHECK_RESULT(result); - } - Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), - Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, - NULL, NULL); -#else - UNREACHABLE(); -#endif // !defined(DART_PRECOMPILED_RUNTIME) } + Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), + Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, + NULL, NULL); +#else + if (!isolate_run_app_snapshot) { + UNREACHABLE(); + } +#endif // !defined(DART_PRECOMPILED_RUNTIME) if (Options::gen_snapshot_kind() == kAppJIT) { // If we sort, we must do it for all isolates, not just the main isolate, @@ -854,8 +793,9 @@ bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) { Options::package_root()); } + const char* packages_config = Options::packages_file(); Dart_Isolate isolate = CreateIsolateGroupAndSetupHelper( - is_main_isolate, script_name, "main", Options::packages_file(), &flags, + is_main_isolate, script_name, "main", packages_config, &flags, NULL /* callback_data */, &error, &exit_code); if (isolate == NULL) { @@ -880,8 +820,6 @@ bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) { Dart_EnterScope(); - auto isolate_group_data = - reinterpret_cast(Dart_IsolateGroupData(isolate)); if (Options::gen_snapshot_kind() == kKernel) { if (vm_run_app_snapshot) { Syslog::PrintErr( @@ -891,7 +829,7 @@ bool RunMainIsolate(const char* script_name, CommandLineOptions* dart_options) { Platform::Exit(kErrorExitCode); } Snapshot::GenerateKernel(Options::snapshot_filename(), script_name, - isolate_group_data->resolved_packages_config()); + packages_config); } else { // Lookup the library of the root script. Dart_Handle root_lib = Dart_RootLibrary(); diff --git a/runtime/include/dart_api.h b/runtime/include/dart_api.h index 56120e78fa4..33b9310b366 100644 --- a/runtime/include/dart_api.h +++ b/runtime/include/dart_api.h @@ -3332,6 +3332,17 @@ DART_EXPORT Dart_Port Dart_KernelPort(); * * \param platform_kernel_size The length of the platform_kernel buffer. * + * \param incremental_compile Specifies if incremental compilation is needed. + * + * \param package_config Uri of the package configuration file (either in format + * of .packages or .dart_tool/package_config.json) for the null safety + * detection to resolve package imports against. If this parameter is not + * passed the package resolution of the parent isolate should be used. + * + * \param original_working_directory current working directory when the VM + * process was launched, this is used to correctly resolve the path specified + * for package_config. + * * \return Returns the result of the compilation. * * On a successful compilation the returned [Dart_KernelCompilationResult] has @@ -3349,7 +3360,8 @@ Dart_CompileToKernel(const char* script_uri, const uint8_t* platform_kernel, const intptr_t platform_kernel_size, bool incremental_compile, - const char* package_config); + const char* package_config, + const char* original_working_directory); typedef struct { const char* uri; diff --git a/runtime/vm/dart_api_impl.cc b/runtime/vm/dart_api_impl.cc index b3800b5b45d..060963a2d68 100644 --- a/runtime/vm/dart_api_impl.cc +++ b/runtime/vm/dart_api_impl.cc @@ -5980,7 +5980,8 @@ Dart_CompileToKernel(const char* script_uri, const uint8_t* platform_kernel, intptr_t platform_kernel_size, bool incremental_compile, - const char* package_config) { + const char* package_config, + const char* original_working_directory) { API_TIMELINE_DURATION(Thread::Current()); Dart_KernelCompilationResult result = {}; @@ -5988,9 +5989,9 @@ Dart_CompileToKernel(const char* script_uri, result.status = Dart_KernelCompilationStatus_Unknown; result.error = strdup("Dart_CompileToKernel is unsupported."); #else - result = KernelIsolate::CompileToKernel(script_uri, platform_kernel, - platform_kernel_size, 0, NULL, - incremental_compile, package_config); + result = KernelIsolate::CompileToKernel( + script_uri, platform_kernel, platform_kernel_size, 0, NULL, + incremental_compile, package_config, original_working_directory); if (result.status == Dart_KernelCompilationStatus_Ok) { Dart_KernelCompilationResult accept_result = KernelIsolate::AcceptCompilation(); diff --git a/runtime/vm/isolate_reload.cc b/runtime/vm/isolate_reload.cc index 6c83404d48a..3fcf4374266 100644 --- a/runtime/vm/isolate_reload.cc +++ b/runtime/vm/isolate_reload.cc @@ -1107,7 +1107,7 @@ char* IsolateGroupReloadContext::CompileToKernel(bool force_reload, TransitionVMToNative transition(Thread::Current()); retval = KernelIsolate::CompileToKernel(root_lib_url, nullptr, 0, modified_scripts_count, - modified_scripts, true, nullptr); + modified_scripts, true); } if (retval.status != Dart_KernelCompilationStatus_Ok) { if (retval.kernel != nullptr) { diff --git a/runtime/vm/kernel_isolate.cc b/runtime/vm/kernel_isolate.cc index 947d4688d0d..96dd335533f 100644 --- a/runtime/vm/kernel_isolate.cc +++ b/runtime/vm/kernel_isolate.cc @@ -1001,6 +1001,7 @@ Dart_KernelCompilationResult KernelIsolate::CompileToKernel( Dart_SourceFile source_files[], bool incremental_compile, const char* package_config, + const char* original_working_directory, const char* multiroot_filepaths, const char* multiroot_scheme) { // Start the kernel Isolate if it is not already running. @@ -1026,7 +1027,7 @@ Dart_KernelCompilationResult KernelIsolate::CompileToKernel( kCompileTag, kernel_port, script_uri, platform_kernel, platform_kernel_size, source_file_count, source_files, incremental_compile, package_config, multiroot_filepaths, - multiroot_scheme, experimental_flags_, NULL); + multiroot_scheme, experimental_flags_, original_working_directory); } bool KernelIsolate::DetectNullSafety(const char* script_uri, diff --git a/runtime/vm/kernel_isolate.h b/runtime/vm/kernel_isolate.h index fbf31e237da..132c18cf550 100644 --- a/runtime/vm/kernel_isolate.h +++ b/runtime/vm/kernel_isolate.h @@ -51,6 +51,7 @@ class KernelIsolate : public AllStatic { Dart_SourceFile source_files[] = NULL, bool incremental_compile = true, const char* package_config = NULL, + const char* original_working_directory = NULL, const char* multiroot_filepaths = NULL, const char* multiroot_scheme = NULL); diff --git a/runtime/vm/unit_test.cc b/runtime/vm/unit_test.cc index bcc7d99bd96..fcff39904ec 100644 --- a/runtime/vm/unit_test.cc +++ b/runtime/vm/unit_test.cc @@ -326,7 +326,8 @@ char* TestCase::CompileTestScriptWithDFE(const char* url, Zone* zone = Thread::Current()->zone(); Dart_KernelCompilationResult result = KernelIsolate::CompileToKernel( url, platform_strong_dill, platform_strong_dill_size, sourcefiles_count, - sourcefiles, incrementally, NULL, multiroot_filepaths, multiroot_scheme); + sourcefiles, incrementally, nullptr, nullptr, multiroot_filepaths, + multiroot_scheme); if (result.status == Dart_KernelCompilationStatus_Ok) { if (KernelIsolate::AcceptCompilation().status != Dart_KernelCompilationStatus_Ok) { diff --git a/sdk/lib/_internal/vm/bin/builtin.dart b/sdk/lib/_internal/vm/bin/builtin.dart index e54b92b1f62..a86de3859b3 100644 --- a/sdk/lib/_internal/vm/bin/builtin.dart +++ b/sdk/lib/_internal/vm/bin/builtin.dart @@ -19,10 +19,6 @@ import 'dart:typed_data'; // command line. bool _traceLoading = false; -// Before handling an embedder entrypoint we finalize the setup of the -// dart:_builtin library. -bool _setupCompleted = false; - // 'print' implementation. // The standalone embedder registers the closurized _print function with the // dart:core library. @@ -95,17 +91,6 @@ _sanitizeWindowsPath(path) { return fixedPath; } -_setPackagesConfig(String packagesParam) { - var packagesName = _sanitizeWindowsPath(packagesParam); - var packagesUri = Uri.parse(packagesName); - if (packagesUri.scheme == '') { - // Script does not have a scheme, assume that it is a path, - // resolve it against the working directory. - packagesUri = _workingDirectory.resolveUri(packagesUri); - } - _packagesConfigUri = packagesUri; -} - // Given a uri with a 'package' scheme, return a Uri that is prefixed with // the package root or resolved relative to the package configuration. Uri _resolvePackageUri(Uri uri) { @@ -486,60 +471,53 @@ _handlePackagesRequest(bool traceLoading, int tag, Uri resource) { // Embedder Entrypoint: // The embedder calls this method to initial the package resolution state. +// Returns the resolved script URI after it resolves the script uri in the +// current working directory iff the given uri did not specify a scheme +// (e.g. a path to a script file on the command line). @pragma("vm:entry-point") -void _Init(String packagesConfig, String workingDirectory, String rootScript) { +String _Init( + String packagesConfig, String workingDirectory, String rootScript) { + if (_traceLoading) { + _log("_Init: $packagesConfig $workingDirectory $rootScript"); + } + // Register callbacks and hooks with the rest of core libraries. _setupHooks(); // _workingDirectory must be set first. _workingDirectory = new Uri.directory(workingDirectory); + if (_traceLoading) { + _log('_Init (working directory): $_workingDirectory'); + } // setup _rootScript. if (rootScript != null) { - _rootScript = Uri.parse(rootScript); + _rootScript = _resolveScriptUri(rootScript); + assert(rootScript != null); } // If the --packages flag was passed, setup _packagesConfig. if (packagesConfig != null) { _packageMap = null; - _setPackagesConfig(packagesConfig); + VMLibraryHooks.packageConfigString = _setPackagesConfig(packagesConfig); } + + if (_traceLoading) { + _log("_Init returns: ${rootScript.toString()}"); + } + return _rootScript.toString(); } -// Embedder Entrypoint: -// The embedder calls this method with the current working directory. -@pragma("vm:entry-point") -void _setWorkingDirectory(String cwd) { - if (!_setupCompleted) { - _setupHooks(); - } - if (_traceLoading) { - _log('Setting working directory: $cwd'); - } - _workingDirectory = new Uri.directory(cwd); - if (_traceLoading) { - _log('Working directory URI: $_workingDirectory'); - } -} - -// Embedder Entrypoint: -// The embedder calls this method with the value of the --packages command line -// option. It can point to a ".packages" or a ".dart_tool/package_config.json" +// packagesParam is the value of the --packages command line option. +// It can point to a ".packages" or a ".dart_tool/package_config.json" // file. -@pragma("vm:entry-point") -String _setPackagesMap(String packagesParam) { - if (!_setupCompleted) { - _setupHooks(); - } +String _setPackagesConfig(String packagesParam) { // First convert the packages parameter from the command line to a URI which // can be handled by the loader code. // TODO(iposva): Consider refactoring the common code below which is almost // shared with resolution of the root script. if (_traceLoading) { - _log("Resolving packages map: $packagesParam"); - } - if (_workingDirectory == null) { - throw 'No current working directory set.'; + _log("Resolving packages config: $packagesParam"); } var packagesName = _sanitizeWindowsPath(packagesParam); var packagesUri = Uri.parse(packagesName); @@ -548,24 +526,19 @@ String _setPackagesMap(String packagesParam) { // resolve it against the working directory. packagesUri = _workingDirectory.resolveUri(packagesUri); } - var packagesUriStr = packagesUri.toString(); - VMLibraryHooks.packageConfigString = packagesUriStr; + _packagesConfigUri = packagesUri; if (_traceLoading) { - _log('Resolved packages map to: $packagesUri'); + _log('Resolved packages config to: $packagesUri'); } - return packagesUriStr; + return packagesUri.toString(); } // Resolves the script uri in the current working directory iff the given uri // did not specify a scheme (e.g. a path to a script file on the command line). -@pragma("vm:entry-point") -String _resolveScriptUri(String scriptName) { +Uri _resolveScriptUri(String scriptName) { if (_traceLoading) { _log("Resolving script: $scriptName"); } - if (_workingDirectory == null) { - throw 'No current working directory set.'; - } scriptName = _sanitizeWindowsPath(scriptName); var scriptUri = Uri.parse(scriptName); @@ -575,20 +548,15 @@ String _resolveScriptUri(String scriptName) { scriptUri = _workingDirectory.resolveUri(scriptUri); } - // Remember the root script URI so that we can resolve packages based on - // this location. - _rootScript = scriptUri; - if (_traceLoading) { _log('Resolved entry point to: $_rootScript'); } - return scriptUri.toString(); + return scriptUri; } // Register callbacks and hooks with the rest of the core libraries. @pragma("vm:entry-point") _setupHooks() { - _setupCompleted = true; VMLibraryHooks.packageConfigUriFuture = _getPackageConfigFuture; VMLibraryHooks.resolvePackageUriFuture = _resolvePackageUriFuture; } diff --git a/sdk_nnbd/lib/_internal/vm/bin/builtin.dart b/sdk_nnbd/lib/_internal/vm/bin/builtin.dart index 59f939210a5..8dcbadf4e7f 100644 --- a/sdk_nnbd/lib/_internal/vm/bin/builtin.dart +++ b/sdk_nnbd/lib/_internal/vm/bin/builtin.dart @@ -17,10 +17,6 @@ import 'dart:typed_data'; // command line. bool _traceLoading = false; -// Before handling an embedder entrypoint we finalize the setup of the -// dart:_builtin library. -bool _setupCompleted = false; - // 'print' implementation. // The standalone embedder registers the closurized _print function with the // dart:core library. @@ -93,17 +89,6 @@ _sanitizeWindowsPath(path) { return fixedPath; } -_setPackagesConfig(String packagesParam) { - var packagesName = _sanitizeWindowsPath(packagesParam); - var packagesUri = Uri.parse(packagesName); - if (packagesUri.scheme == '') { - // Script does not have a scheme, assume that it is a path, - // resolve it against the working directory. - packagesUri = _workingDirectory.resolveUri(packagesUri); - } - _packagesConfigUri = packagesUri; -} - // Given a uri with a 'package' scheme, return a Uri that is prefixed with // the package root or resolved relative to the package configuration. Uri _resolvePackageUri(Uri uri) { @@ -485,57 +470,53 @@ _handlePackagesRequest(bool traceLoading, int tag, Uri resource) { // Embedder Entrypoint: // The embedder calls this method to initial the package resolution state. +// Returns the resolved script URI after it resolves the script uri in the +// current working directory iff the given uri did not specify a scheme +// (e.g. a path to a script file on the command line). @pragma("vm:entry-point") -void _Init(String packagesConfig, String workingDirectory, String rootScript) { +String _Init( + String packagesConfig, String workingDirectory, String rootScript) { + if (_traceLoading) { + _log("_Init: $packagesConfig $workingDirectory $rootScript"); + } + // Register callbacks and hooks with the rest of core libraries. _setupHooks(); // _workingDirectory must be set first. _workingDirectory = new Uri.directory(workingDirectory); + if (_traceLoading) { + _log('_Init (working directory): $_workingDirectory'); + } // setup _rootScript. if (rootScript != null) { - _rootScript = Uri.parse(rootScript); + _rootScript = _resolveScriptUri(rootScript); + assert(rootScript != null); } // If the --packages flag was passed, setup _packagesConfig. if (packagesConfig != null) { _packageMap = null; - _setPackagesConfig(packagesConfig); + VMLibraryHooks.packageConfigString = _setPackagesConfig(packagesConfig); } + + if (_traceLoading) { + _log("_Init returns: ${rootScript.toString()}"); + } + return _rootScript.toString(); } -// Embedder Entrypoint: -// The embedder calls this method with the current working directory. -@pragma("vm:entry-point") -void _setWorkingDirectory(String cwd) { - if (!_setupCompleted) { - _setupHooks(); - } - if (_traceLoading) { - _log('Setting working directory: $cwd'); - } - _workingDirectory = new Uri.directory(cwd); - if (_traceLoading) { - _log('Working directory URI: $_workingDirectory'); - } -} - -// Embedder Entrypoint: -// The embedder calls this method with the value of the --packages command line -// option. It can point to a ".packages" or a ".dart_tool/package_config.json" +// packagesParam is the value of the --packages command line option. +// It can point to a ".packages" or a ".dart_tool/package_config.json" // file. -@pragma("vm:entry-point") -String _setPackagesMap(String packagesParam) { - if (!_setupCompleted) { - _setupHooks(); - } +String _setPackagesConfig(String packagesParam) { // First convert the packages parameter from the command line to a URI which // can be handled by the loader code. // TODO(iposva): Consider refactoring the common code below which is almost // shared with resolution of the root script. if (_traceLoading) { - _log("Resolving packages map: $packagesParam"); + _log("Resolving packages config: $packagesParam"); } var packagesName = _sanitizeWindowsPath(packagesParam); var packagesUri = Uri.parse(packagesName); @@ -544,18 +525,16 @@ String _setPackagesMap(String packagesParam) { // resolve it against the working directory. packagesUri = _workingDirectory.resolveUri(packagesUri); } - var packagesUriStr = packagesUri.toString(); - VMLibraryHooks.packageConfigString = packagesUriStr; + _packagesConfigUri = packagesUri; if (_traceLoading) { - _log('Resolved packages map to: $packagesUri'); + _log('Resolved packages config to: $packagesUri'); } - return packagesUriStr; + return packagesUri.toString(); } // Resolves the script uri in the current working directory iff the given uri // did not specify a scheme (e.g. a path to a script file on the command line). -@pragma("vm:entry-point") -String _resolveScriptUri(String scriptName) { +Uri _resolveScriptUri(String scriptName) { if (_traceLoading) { _log("Resolving script: $scriptName"); } @@ -568,20 +547,15 @@ String _resolveScriptUri(String scriptName) { scriptUri = _workingDirectory.resolveUri(scriptUri); } - // Remember the root script URI so that we can resolve packages based on - // this location. - _rootScript = scriptUri; - if (_traceLoading) { _log('Resolved entry point to: $_rootScript'); } - return scriptUri.toString(); + return scriptUri; } // Register callbacks and hooks with the rest of the core libraries. @pragma("vm:entry-point") _setupHooks() { - _setupCompleted = true; VMLibraryHooks.packageConfigUriFuture = _getPackageConfigFuture; VMLibraryHooks.resolvePackageUriFuture = _resolvePackageUriFuture; }