From b0bc023f244330427876c5cd46ea80a5c0badae6 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Thu, 2 Nov 2023 17:42:25 -0700 Subject: [PATCH] [flutter_tools] do not try to build tool from dart.sh (#129186) Fixes https://github.com/flutter/flutter/issues/121894 --- bin/internal/shared.bat | 1 + bin/internal/shared.sh | 9 ++- .../bash_entrypoint_test.dart | 63 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/bin/internal/shared.bat b/bin/internal/shared.bat index 421868a9ee58..97eee5a9b337 100644 --- a/bin/internal/shared.bat +++ b/bin/internal/shared.bat @@ -130,6 +130,7 @@ GOTO :after_subroutine IF EXIST "%FLUTTER_ROOT%\version" DEL "%FLUTTER_ROOT%\version" IF EXIST "%FLUTTER_ROOT%\bin\cache\flutter.version.json" DEL "%FLUTTER_ROOT%\bin\cache\flutter.version.json" ECHO: > "%cache_dir%\.dartignore" + ECHO Building flutter tool... 1>&2 PUSHD "%flutter_tools_dir%" diff --git a/bin/internal/shared.sh b/bin/internal/shared.sh index 75d9d3013eb2..2a47642b16f8 100644 --- a/bin/internal/shared.sh +++ b/bin/internal/shared.sh @@ -3,7 +3,6 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. - # ---------------------------------- NOTE ---------------------------------- # # # Please keep the logic in this file consistent with the logic in the @@ -144,6 +143,11 @@ function upgrade_flutter () ( touch "$FLUTTER_ROOT/bin/cache/.dartignore" "$FLUTTER_ROOT/bin/internal/update_dart_sdk.sh" + if [[ "$BIN_NAME" == 'dart' ]]; then + # Don't try to build tool + return + fi + >&2 echo Building flutter tool... # Prepare packages... @@ -229,6 +233,8 @@ function shared::execute() { exit 1 fi + BIN_NAME="$(basename "$PROG_NAME")" + # File descriptor 7 is prepared here so that we can use it with # flock(1) in _lock() (see above). # @@ -247,7 +253,6 @@ function shared::execute() { # SHARED_NAME itself is prepared by the caller script. upgrade_flutter 7< "$SHARED_NAME" - BIN_NAME="$(basename "$PROG_NAME")" case "$BIN_NAME" in flutter*) # FLUTTER_TOOL_ARGS aren't quoted below, because it is meant to be diff --git a/packages/flutter_tools/test/integration.shard/bash_entrypoint_test.dart b/packages/flutter_tools/test/integration.shard/bash_entrypoint_test.dart index 7377889c24ef..7fd91cbc4bd2 100644 --- a/packages/flutter_tools/test/integration.shard/bash_entrypoint_test.dart +++ b/packages/flutter_tools/test/integration.shard/bash_entrypoint_test.dart @@ -48,6 +48,64 @@ Future main() async { expect(stdout, contains('Successfully received SIGTERM!')); }, skip: platform.isWindows); // [intended] Windows does not use the bash entrypoint + + test('shared.sh does not compile flutter tool if PROG_NAME=dart', () async { + final Directory tempDir = fileSystem.systemTempDirectory.createTempSync('bash_entrypoint_test'); + try { + // bash script checks it is in a git repo + ProcessResult result = await processManager.run(['git', 'init'], workingDirectory: tempDir.path); + expect(result, const ProcessResultMatcher()); + result = await processManager.run(['git', 'commit', '--allow-empty', '-m', 'init commit'], workingDirectory: tempDir.path); + expect(result, const ProcessResultMatcher()); + + // copy dart and shared.sh to temp dir + final File trueSharedSh = flutterRoot.childDirectory('bin').childDirectory('internal').childFile('shared.sh'); + final File fakeSharedSh = (tempDir.childDirectory('bin').childDirectory('internal') + ..createSync(recursive: true)) + .childFile('shared.sh'); + trueSharedSh.copySync(fakeSharedSh.path); + final File fakeDartBash = tempDir.childDirectory('bin').childFile('dart'); + dartBash.copySync(fakeDartBash.path); + // mark dart executable + makeExecutable(fakeDartBash); + + // create no-op fake update_dart_sdk.sh script + final File updateDartSdk = tempDir.childDirectory('bin').childDirectory('internal').childFile('update_dart_sdk.sh')..writeAsStringSync(''' +#!/usr/bin/env bash + +echo downloaded dart sdk +'''); + makeExecutable(updateDartSdk); + + // create a fake dart runtime + final File dartBin = (tempDir.childDirectory('bin') + .childDirectory('cache') + .childDirectory('dart-sdk') + .childDirectory('bin') + ..createSync(recursive: true)) + .childFile('dart'); + dartBin.writeAsStringSync(''' +#!/usr/bin/env bash + +echo executed dart binary +'''); + makeExecutable(dartBin); + + result = await processManager.run([fakeDartBash.path]); + expect(result, const ProcessResultMatcher()); + expect( + (result.stdout as String).split('\n'), + // verify we ran updateDartSdk and dartBin + containsAll(['downloaded dart sdk', 'executed dart binary']), + ); + + // Verify we did not try to compile the flutter_tool + expect(result.stderr, isNot(contains('Building flutter tool...'))); + } finally { + tryToDelete(tempDir); + } + }, + skip: platform.isWindows); // [intended] Windows does not use the bash entrypoint } // A test Dart app that will run until it receives SIGTERM @@ -69,3 +127,8 @@ File get dartBash { .childFile('dart') .absolute; } + +void makeExecutable(File file) { + final ProcessResult result = processManager.runSync(['chmod', '+x', file.path]); + expect(result, const ProcessResultMatcher()); +}