melos icon indicating copy to clipboard operation
melos copied to clipboard

fix: Env variables not available (except for MELOS_ROOT_PATH)

Open wujek-srujek opened this issue 10 months ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

Version

3.1.1

Description

According to the docs https://melos.invertase.dev/~melos-latest/environment-variables some env variables should be available to scripts, but I can only ever read MELOD_ROOT_PATH. For example in my project, malos.yaml contains this bit:

scripts:
  echo:vars:
    exec: echo $MELOS_ROOT_PATH $MELOS_PACKAGE_NAME $MELOS_PACKAGE_VERSION $MELOS_PACKAGE_PATH

  env:
    exec: env | grep -i melos

When I run melos run env I see the promised variables, but when I run melos run echo:vars the only one that is printed out is MELOS_ROOT_PATH. How does this work?

Steps to reproduce

Create a project with Melos and use the code snippet mentioned in the description.

Expected behavior

melos run echo:vars prints out the variable values, or the documentation is clearer about when the variables are available.

Screenshots

No response

Additional context and comments

No response

wujek-srujek avatar Aug 23 '23 17:08 wujek-srujek

I have experienced the same problem with $MELOS_PACKAGE_PATH appearing empty. The example echo:vars script:

scripts:
  echo:vars:
    exec: echo $MELOS_ROOT_PATH $MELOS_PACKAGE_NAME $MELOS_PACKAGE_VERSION $MELOS_PACKAGE_PATH

only outputs $MELOS_ROOT_PATH value for me as well. However, prepending $MELOS_PACKAGE_PATH and others with \ fixes the situation and produces the expected output:

scripts:
  echo:vars:
    exec: echo \$MELOS_ROOT_PATH \$MELOS_PACKAGE_NAME \$MELOS_PACKAGE_VERSION \$MELOS_PACKAGE_PATH

foxanna avatar Nov 01 '23 16:11 foxanna

The shell is trying to resolve anything with $ in front of it as a variable before it even gets to Melos, so when it gets to Melos it's an empty string since the shell variable does not exist,

Melos also supports these without the $ so you don't have to worry about escaping it with \ etc, e.g.:

scripts:
  echo:vars:
    exec: echo MELOS_ROOT_PATH MELOS_PACKAGE_NAME MELOS_PACKAGE_VERSION MELOS_PACKAGE_PATH

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[firebaseapis]: /Users/mike/development/projects/dart_firebase_apis firebaseapis 0.2.0 /Users/mike/development/projects/dart_firebase_apis/generated/firebaseapis
[sample]: /Users/mike/development/projects/dart_firebase_apis sample 0.0.0 /Users/mike/development/projects/dart_firebase_apis/resources/firebaseapis
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

$ melos exec
  â””> echo /Users/mike/development/projects/dart_firebase_apis MELOS_PACKAGE_NAME MELOS_PACKAGE_VERSION MELOS_PACKAGE_PATH
     â””> SUCCESS

I wonder if we should just have this as the default way that is mentioned in the docs so no shell knowledge is required, wdyt?

Salakar avatar Nov 06 '23 15:11 Salakar