Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows]: Programs installed with pub global activate run twice #2934

Closed
Levi-Lesches opened this issue Mar 24, 2021 · 5 comments · Fixed by #3055
Closed

[Windows]: Programs installed with pub global activate run twice #2934

Levi-Lesches opened this issue Mar 24, 2021 · 5 comments · Fixed by #3055

Comments

@Levi-Lesches
Copy link
Contributor

Environment

  • pub version or flutter pub version: Pub 2.12.2
  • OS version: Windows 10
  • Are you using the Chinese community mirror or a corporate firewall? No

Problem

When running an app installed with pub global activate, the program runs twice, except if executed with pub global run.

Expected behavior

> pub global activate pana
> pana Could not find an option or flag "-h".

Usage: pana [<options>] --hosted <published package name> [<version>]
       pana [<options>] <local directory>

Options:
      --flutter-sdk     The directory of the Flutter SDK.
  -j, --json            Output log records and full report as JSON.
      --hosted-url      The server that hosts <package>.
                        (defaults to "https://pub.dev")
  -l, --line-length     The line length to use with dartfmt.
      --hosted          Download and analyze a hosted package (from https://pub.dev).
      --[no-]warning    Shows the warning message before potentially destructive operation.
                        (defaults to on)

Actual behavior

> pub global activate pana
> pana Could not find an option or flag "-h".

Usage: pana [<options>] --hosted <published package name> [<version>]
       pana [<options>] <local directory>

Options:
      --flutter-sdk     The directory of the Flutter SDK.
  -j, --json            Output log records and full report as JSON.
      --hosted-url      The server that hosts <package>.
                        (defaults to "https://pub.dev")
  -l, --line-length     The line length to use with dartfmt.
      --hosted          Download and analyze a hosted package (from https://pub.dev).
      --[no-]warning    Shows the warning message before potentially destructive operation.
                        (defaults to on)
Could not find an option or flag "-h".

Usage: pana [<options>] --hosted <published package name> [<version>]
       pana [<options>] <local directory>

Options:
      --flutter-sdk     The directory of the Flutter SDK.
  -j, --json            Output log records and full report as JSON.
      --hosted-url      The server that hosts <package>.
                        (defaults to "https://pub.dev")
  -l, --line-length     The line length to use with dartfmt.
      --hosted          Download and analyze a hosted package (from https://pub.dev).
      --[no-]warning    Shows the warning message before potentially destructive operation.
                        (defaults to on)-h  

The problem:

Using pana as an example (it happens with every package), I can find the location of the batch file using where:

C:\Users\user\AppData\Roaming\Pub\Cache\bin\pana.bat

Inspecting the file, I found the call was made twice:

@echo off
rem This file was created by pub v2.12.2.
rem Package: pana
rem Version: 0.15.4
rem Executable: pana
rem Script: pana
if exist "C:\Users\levi\AppData\Roaming\Pub\Cache\global_packages\pana\bin\pana.dart-2.12.2.snapshot" (
  dart "C:\Users\levi\AppData\Roaming\Pub\Cache\global_packages\pana\bin\pana.dart-2.12.2.snapshot" %*
  rem The VM exits with code 253 if the snapshot version is out-of-date.
  rem If it is, we need to delete it and run "pub global" manually.
  if not errorlevel 253 (
    goto error
  )
  pub global run pana:pana %* 
) else (
  pub global run pana:pana %*
)
goto eof
:error
exit /b %errorlevel%
:eof

As you can see, it calls dart <path_to_pana>, and then if there's an error, it has a goto. So the immediate call to pub should not be there. I removed it and it worked just fine.

@Levi-Lesches Levi-Lesches changed the title [Windows]: [Windows]: Programs installed with pub global activate run twice Mar 24, 2021
@Levi-Lesches
Copy link
Contributor Author

Levi-Lesches commented Mar 24, 2021

It seems the issue can be solved by deleting this line:

pub global run ${package.name}:$script %*

EDIT: CORRECTION: THAT LINE IS NOT THE PROBLEM. While it does fix this, if your package's snapshot is out-of-date, that line is needed. The correct fix was adding a call statement to the line that invokes Dart. Without it, the Batch script would miss the goto error line (idk why) and hit the dart pub global run line when it really should have exited. I edited the PR though, so you can still watch that.

@leoafarias
Copy link

leoafarias commented Apr 1, 2021

The same issue was happening for users that have flutter/bin/cache/dart-sdk/bin in the environment variable.

After removing this path, started to have pub is not recognized as an internal or external command dart-lang/sdk#43434

The issue went away completely after doing dart global activate <package>

Not sure how to coordinate but seems that there are a few moving parts that need to get tackled including the following item here.

flutter/flutter#57518 so the flutter-tools can notify about the cache/dart-sdk/bin in the PATH

@rydmike
Copy link

rydmike commented Apr 1, 2021

Been seeing this for quite some time, and thought it was on issue with fvm, this explains it though.
Tinkered with the order for paths in windows here leoafarias/fvm#227.
At least for now that has helped, no idea if it has other side effects or if the workaround will continue to work.

@Levi-Lesches
Copy link
Contributor Author

The same issue was happening for users that have flutter/bin/cache/dart-sdk/bin in the environment variable.

That's where my (and many others') Dart SDK is -- I didn't install it separately, I use the version bundled with Flutter.

When I inspected pub's global activate code, I found the line (above) that I believe was incorrectly added and calls the downloaded package/executable twice. I think I'll open a PR to remove it and/or investigate whether there's supposed to be an else on that line instead.

@Levi-Lesches
Copy link
Contributor Author

Levi-Lesches commented Jul 27, 2021

It seems the issue can be solved by deleting this line:

pub global run ${package.name}:$script %*

Well I just saw this error again four months later so I opened a PR to delete that line: #3055

EDIT CORRECTION: THAT LINE IS NOT THE PROBLEM. While it does fix this, if your package's snapshot is out-of-date, that line is needed. The correct fix was adding a call statement to the line that invokes Dart. Without it, the Batch script would miss the goto error line (idk why) and hit the dart pub global run line when it really should have exited. I edited the PR though, so you can still watch that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants