che icon indicating copy to clipboard operation
che copied to clipboard

Eclipse Che does not properly process `$(PROJECT_SOURCE)` in the `env` section of a container component

Open cgruver opened this issue 1 year ago • 5 comments

Describe the bug

See the devfile.io documentation on Environment Variables:

https://devfile.io/docs/2.2.2/defining-environment-variables

Eclipse Che does not appear to be properly converting $(PROJECT_SOURCE) if specified in a devfile.

Che version

7.87@latest

Steps to reproduce

Create a simple devfile including the following snippet in the container component:

    env:
    - name: ENV_TEST
      value: $(PROJECT_SOURCE)/hey-there

Start a workspace from the devfile and open a terminal.

Observe that the environment variable was interpreted literally:

echo $ENV_TEST
$(PROJECT_SOURCE)/hey-there

Expected behavior

Eclipse Che should convert $(PROJECT_SOURCE) to the path of the cloned code.

Runtime

OpenShift

Screenshots

No response

Installation method

OperatorHub

Environment

macOS

Eclipse Che Logs

No response

Additional context

No response

cgruver avatar Jun 13 '24 11:06 cgruver

@cgruver I just quickly looked into this and was able to reproduce this issue. However, the following should work:

    env:
    - name: ENV_TEST
      value: ${PROJECT_SOURCE}/hey-there

The DevFile documentation to use $(...) instead of ${...} seems a bit odd as ${} is used for variable substitution and $PROJECT_SOURCE is an environment variable. $() is used for evaluating a command, e.g. $(pwd)/hey-there.

Maybe one could argue that this is a bug with the https://github.com/devfile/docs?

AObuchow avatar Jun 13 '24 14:06 AObuchow

@AObuchow I tried ${..}. It does not work either.

echo $ENV_TEST
${PROJECT_SOURCE}/hey-there

It appears as though the input string is interpreted as a literal and does not process the $.

cgruver avatar Jun 13 '24 16:06 cgruver

@cgruver are you running your echo command in the terminal or as a CheCode command/task?

AObuchow avatar Jun 13 '24 16:06 AObuchow

Terminal in the workspace.

cgruver avatar Jun 13 '24 16:06 cgruver

@cgruver Thanks for the info.

I think I found the source of this bug: The user-provided environment variable ENV_TEST is being added to the container's environment variables before $PROJECT_SOURCE. Kubernetes can only evaluate environment variables that reference other environment variables if the referenced environment variable arrives first in the list of environment variables.

In other words, for the workspace container's spec we have something like:

    env:
    - name: ENV_TEST
      value: $(PROJECT_SOURCE)/hey-there
    - name: PROJECTS_ROOT
      value: /projects
    - name: PROJECT_SOURCE
      value: /projects/web-nodejs-whatever

When it should be:

    env:
    - name: PROJECTS_ROOT
      value: /projects
    - name: PROJECT_SOURCE
      value: /projects/web-nodejs-whatever
    - name: ENV_TEST # Must be placed after $PROJECT_SOURCE is declared
      value: $(PROJECT_SOURCE)/hey-there

Also, using $() is the proper syntax to use here for kubernetes to evaluate the referenced environment variable. My mistake.

I'll report this as a DevWorkspace Operator bug.

AObuchow avatar Jun 14 '24 19:06 AObuchow

The DWO issue causing this bug has been resolved in https://github.com/devfile/devworkspace-operator/pull/1282 which will be part of the DWO 0.30.x release.

AObuchow avatar Jul 03 '24 23:07 AObuchow