Skip to content

Commit

Permalink
[Windows] Remove the usage of SETLOCAL ENABLEDELAYEDEXPANSION from …
Browse files Browse the repository at this point in the history
…bat scripts. (flutter#106861)
  • Loading branch information
moko256 authored Jul 14, 2022
1 parent d32aa8b commit 45e212e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
6 changes: 4 additions & 2 deletions bin/dart.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ REM work across all platforms!
REM
REM --------------------------------------------------------------------------

SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL

FOR %%i IN ("%~dp0..") DO SET FLUTTER_ROOT=%%~fi

Expand All @@ -23,6 +23,8 @@ SET cache_dir=%FLUTTER_ROOT%\bin\cache
SET dart_sdk_path=%cache_dir%\dart-sdk
SET dart=%dart_sdk_path%\bin\dart.exe

SET exit_with_errorlevel=%FLUTTER_ROOT%/bin/internal/exit_with_errorlevel.bat

REM Chaining the call to 'dart' and 'exit' with an ampersand ensures that
REM Windows reads both commands into memory once before executing them. This
REM avoids nasty errors that may otherwise occur when the dart command (e.g. as
Expand All @@ -31,4 +33,4 @@ REM
REM Do not use the CALL command in the next line to execute Dart. CALL causes
REM Windows to re-read the line from disk after the CALL command has finished
REM regardless of the ampersand chain.
"%dart%" %* & exit /B !ERRORLEVEL!
"%dart%" %* & "%exit_with_errorlevel%"
6 changes: 4 additions & 2 deletions bin/flutter.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ REM work across all platforms!
REM
REM --------------------------------------------------------------------------

SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL

REM To debug the tool, you can uncomment the following line to enable debug mode:
REM SET FLUTTER_TOOL_ARGS="--enable-asserts %FLUTTER_TOOL_ARGS%"
Expand Down Expand Up @@ -43,6 +43,8 @@ SET snapshot_path=%cache_dir%\flutter_tools.snapshot
SET dart_sdk_path=%cache_dir%\dart-sdk
SET dart=%dart_sdk_path%\bin\dart.exe

SET exit_with_errorlevel=%FLUTTER_ROOT%/bin/internal/exit_with_errorlevel.bat

REM Chaining the call to 'dart' and 'exit' with an ampersand ensures that
REM Windows reads both commands into memory once before executing them. This
REM avoids nasty errors that may otherwise occur when the dart command (e.g. as
Expand All @@ -51,4 +53,4 @@ REM
REM Do not use the CALL command in the next line to execute Dart. CALL causes
REM Windows to re-read the line from disk after the CALL command has finished
REM regardless of the ampersand chain.
"%dart%" --disable-dart-dev --packages="%flutter_tools_dir%\.dart_tool\package_config.json" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %* & exit /B !ERRORLEVEL!
"%dart%" --disable-dart-dev --packages="%flutter_tools_dir%\.dart_tool\package_config.json" %FLUTTER_TOOL_ARGS% "%snapshot_path%" %* & "%exit_with_errorlevel%"
13 changes: 13 additions & 0 deletions bin/internal/exit_with_errorlevel.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@ECHO off
REM Copyright 2014 The Flutter Authors. All rights reserved.
REM Use of this source code is governed by a BSD-style license that can be
REM found in the LICENSE file.

REM A script to exit caller script with the last status code.
REM This can be used with ampersand without `SETLOCAL ENABLEDELAYEDEXPANSION`.
REM
REM To use this script like `exit`, do not use with the CALL command.
REM Without CALL, this script can exit caller script, but with CALL,
REM this script returns back to caller and does not exit caller.

exit /B %ERRORLEVEL%
8 changes: 4 additions & 4 deletions bin/internal/shared.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ REM work across all platforms!
REM
REM --------------------------------------------------------------------------

SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL

SET flutter_tools_dir=%FLUTTER_ROOT%\packages\flutter_tools
SET cache_dir=%FLUTTER_ROOT%\bin\cache
Expand Down Expand Up @@ -84,11 +84,11 @@ GOTO :after_subroutine
IF NOT EXIST "%engine_stamp%" GOTO do_sdk_update_and_snapshot
SET /P dart_required_version=<"%engine_version_path%"
SET /P dart_installed_version=<"%engine_stamp%"
IF !dart_required_version! NEQ !dart_installed_version! GOTO do_sdk_update_and_snapshot
IF %dart_required_version% NEQ %dart_installed_version% GOTO do_sdk_update_and_snapshot
IF NOT EXIST "%snapshot_path%" GOTO do_snapshot
IF NOT EXIST "%stamp_path%" GOTO do_snapshot
SET /P stamp_value=<"%stamp_path%"
IF !stamp_value! NEQ !compilekey! GOTO do_snapshot
IF %stamp_value% NEQ %compilekey% GOTO do_snapshot
SET pubspec_yaml_path=%flutter_tools_dir%\pubspec.yaml
SET pubspec_lock_path=%flutter_tools_dir%\pubspec.lock
FOR /F %%i IN ('DIR /B /O:D "%pubspec_yaml_path%" "%pubspec_lock_path%"') DO SET newer_file=%%i
Expand All @@ -104,7 +104,7 @@ GOTO :after_subroutine
ECHO Checking Dart SDK version... 1>&2
SET update_dart_bin=%FLUTTER_ROOT%\bin\internal\update_dart_sdk.ps1
REM Escape apostrophes from the executable path
SET "update_dart_bin=!update_dart_bin:'=''!"
SET "update_dart_bin=%update_dart_bin:'=''%"
REM PowerShell command must have exit code set in order to prevent all non-zero exit codes from being translated
REM into 1. The exit code 2 is used to detect the case where the major version is incorrect and there should be
REM no subsequent retries.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart = 2.8

import 'dart:io';

import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/globals.dart' as globals;

import '../../src/common.dart';
import '../../src/context.dart';

void main() {
setUpAll(() {
Cache.disableLocking();
});

testUsingContext('flutter command can receive `!`, avoiding expansion by cmd.exe', () async {
final String flutterBin = globals.fs.path.join(getFlutterRoot(), 'bin', 'flutter.bat');

final ProcessResult exec = await Process.run(
flutterBin,
<String>[
'!',
],
workingDirectory: Cache.flutterRoot,
);
// If ENABLEDELAYEDEXPANSION is enabled, the argument `!` is removed,
// and flutter runs without any arguments.
expect(exec.exitCode, 64);
expect(exec.stderr, contains('Could not find a command named "!"'));
},
skip: !Platform.isWindows, // [intended] relies on Windows's cmd.exe
);
}

0 comments on commit 45e212e

Please sign in to comment.