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

DevContainer Fails Due to .env Conflicts #3105

Open
JaiPannu-IITI opened this issue Jan 31, 2025 · 17 comments
Open

DevContainer Fails Due to .env Conflicts #3105

JaiPannu-IITI opened this issue Jan 31, 2025 · 17 comments
Labels
bug Something isn't working unapproved Unapproved for Pull Request

Comments

@JaiPannu-IITI
Copy link

JaiPannu-IITI commented Jan 31, 2025

When working with the DevContainer setup for Talawa API, there is an issue with how the .env file is handled during build and startup.

Currently, when we run:

devcontainer build --workspace-folder .

we must manually copy the .env file using:

cp envFiles/.env.devcontainer .env

to ensure the build succeeds.

However, after the build, when running:

devcontainer up --workspace-folder .

it fails because .env already exists, requiring manual deletion before each run.

Similarly, attempting to open the DevContainer in VS Code also fails due to the same .env file conflict.

Steps to reproduce the issue:

  1. Run:

cp envFiles/.env.devcontainer .env
devcontainer build --workspace-folder .

  1. After a successful build, run:

devcontainer up --workspace-folder .

  • The process fails due to .env already existing.
  1. Try opening the DevContainer in VS Code.
  • It also fails for the same reason.
  1. To fix it manually:
  • You need to delete .env before running devcontainer up, which is inconvenient.

Expected Behavior

  • The .env file should be created only when necessary (during the first setup).
  • Restarting the DevContainer with devcontainer up should not require manual .env deletion.
  • VS Code should be able to open the DevContainer without errors related to .env.

Actual Behavior

  • .env must be manually deleted before running the container, or it fails.
  • Opening the DevContainer in VS Code fails due to .env conflicts.

Proposed Fix

Update .devcontainer/devcontainer.json to properly handle .env file creation and avoid manual deletion.

Replace the current initializeCommand:

"initializeCommand": "cp -n ./envFiles/.env.devcontainer ./.env"

With the new command to always overwrite .env:

"initializeCommand": "/bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env && echo \"Overwritten .env\"'"

Why This Fix?

Prevents DevContainer failures → .env is always available.

  • Ensures consistency → No need to manually delete .env before running devcontainer up.
  • Works in all environments → Ensures .env is reset correctly in Docker, CI/CD, and DevContainers.
  • Logs when .env is replaced → Improves transparency.

Screenshot

Image

Additional Details

This fix will improve the DevContainer workflow, preventing unnecessary manual steps and making development more efficient.

@JaiPannu-IITI JaiPannu-IITI added the bug Something isn't working label Jan 31, 2025
@github-actions github-actions bot added the unapproved Unapproved for Pull Request label Jan 31, 2025
@JaiPannu-IITI
Copy link
Author

@palisadoes I believe this is a better alternative. If you agree, please assign me.

@palisadoes
Copy link
Contributor

palisadoes commented Jan 31, 2025

  1. No. For the reasons stated before.
    1. DevContainer Fails Due to .env File Persistence Issues #3102
  2. If you follow the process there isn't an issue
    1. https://docs-api.talawa.io/docs/installation#setup-using-the-cli
  3. Use the latest upstream and you'll see.

@palisadoes palisadoes closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
@palisadoes
Copy link
Contributor

palisadoes commented Jan 31, 2025

Some people are having this issue with node v18. I didn't with node v22.

Can you check?

Reopening

@palisadoes palisadoes reopened this Jan 31, 2025
@JaiPannu-IITI
Copy link
Author

Yes sure

@palisadoes
Copy link
Contributor

Here is the link to another person discovering this:

@JaiPannu-IITI
Copy link
Author

@palisadoes, I would like to know if you are using linux based system? I was also using node v22, it is not the problem.
After some experiments I have found out that running
cp envFiles/.env.devcontainer .env on linux based overwrites the file by default if it is already there, but gives error on macOS and windows.
Equivalent command for windows and macOS would be
/bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env && echo \"Overwritten .env\"'
This also works in all mentioned OS including Linux.

@palisadoes
Copy link
Contributor

It depends on your Linux shell and distro. I'm using kubuntu.

We absolutely don't want to overwrite a preexisting .env file without a user prompt and having a user prompt makes the process non-transparent.

You need to find a flag equivalent to -n that works on all operating systems, possibly -- no-clobber

@palisadoes
Copy link
Contributor

What does the alias command output on your system provide?
There may be an alias for cp that is causing the problem

@palisadoes
Copy link
Contributor

What does alias provide when your shell is sh and not bash

@JaiPannu-IITI
Copy link
Author

JaiPannu-IITI commented Jan 31, 2025

@palisadoes ,

We absolutely don't want to overwrite a preexisting .env file without a user prompt and having a user prompt makes the process non-transparent.

  1. I think this statement is contradicting with current process, right now if env is already there, let's say in your system, then using

cp -n ./envFiles/.env.devcontainer ./.env overwrites the current file with .env.devcontainer(I have tested myself on linux).

  1. using -- no-clobber shows similar behavior as -n. It does not resolve the issue.

  2. On zsh:

jaipannu@Jais-MacBook-Air talawa-api % alias
run-help=man
which-command=whence
jaipannu@Jais-MacBook-Air talawa-api % alias cp
jaipannu@Jais-MacBook-Air talawa-api %

On bash: (No output)

Jais-MacBook-Air:talawa-api jaipannu$ alias
Jais-MacBook-Air:talawa-api jaipannu$ alias cp
bash: alias: cp: not found

On sh: (No output)
sh-3.2$ alias
sh-3.2$ alias cp
sh: alias: cp: not found

Since there was no default alias set for cp, I even tried both interactive and verbose mode, still we have the same error while running script on already made env.

Clarification:

  • Do we want to rewrite the env to default configurations whenever we run container or do we want to retain the configurations changes made by user after or before running devcontainer build --workspace-folder . ?

  • If so, do we want user to know that the changes has been overwritten to factory configurations ?

@palisadoes
Copy link
Contributor

palisadoes commented Jan 31, 2025

You are incorrect. I've tested this multiple times. Your conclusions are based on assumptions due to insufficient testing.

  1. From the cp man page, -n does not overwrite the target file.
-n, --no-clobber
      do not overwrite an existing file and do not fail (overrides a -u or previous -i option). See also --update; equivalent to --update=none.
  1. I tried this with /bin/sh on Kubuntu, and it works according to the man page. We should be using the --update=none flag instead of `-n'.
    Image
  2. You are using cp -f because your system isn't setup correctly, or the shell environment is not right.
  3. Have you tried running the command that is failing exactly as in the error message? When I do that I get this error.
    cp: missing file operand
    Try 'cp --help' for more information.
    
  4. This works. The command needs to be in quotes apparently.
    /bin/sh -c "cp --update=none /tmp/peter2.txt /tmp/peter.txt"
    
  5. Please try the scenarios I cited above to see whether you can reproduce the issue and the fix.

@JaiPannu-IITI
Copy link
Author

@palisadoes, Apologies for the confusion.

I went through the complete updated installation process and have 2 major observations.

  1. Since the discussed problems in old installation process related to connectivity has been resolved, now user does not require to modify any parameter in .env and connection to DB's are established without fail.
    (https://palisadoes-foundation.slack.com/archives/CSWH4PN0M/p1738274538533179?thread_ts=1738255683.726519&cid=CSWH4PN0M)

  2. The only blocker we have currently is the "initializeCommand" cp -n ./envFiles/.env.devcontainer ./.env
    As now our env and installation process are all synchronized. User doesn't require to do any changes in env before running container. Database will automatically connect with factory env.

Now to fix "initializeCommand", we have two approaches

  1. Update installation guidelines making sure developer modify the shell environment and configurations before proceeding to run container so that the current "initializeCommand" runs without fail.

  2. Instead use another command which works across all platforms and handle the env in similar way to above command.
    (i.e., if run on already existing .env, would rewrite it by .env.devcontainer , this is what cp -n ./envFiles/.env.devcontainer ./.env is doing right now behind the scenes but only in Linux operating systems, fails in macOS)

But since macOS utilizes BSD cp, which does not support --update=none as confirmed in the official documentation:
https://ss64.com/mac/cp.html

I can definitively confirm that:
/bin/sh -c "cp --update=none ./envFiles/.env.devcontainer ./.env"
This wouldn't work for anyone using default macOS configurations.

Here’s a reference screenshot for further clarity:
Image

I believe we can develop a better alternative with your guidance. Please tell me how you want to proceed from here so that I can work in that direction.

@palisadoes
Copy link
Contributor

Does this work in MacOS?

/bin/sh -c "cp -n ./envFiles/.env.devcontainer ./.env"

@JaiPannu-IITI
Copy link
Author

JaiPannu-IITI commented Jan 31, 2025

@palisadoes
Code you provided throws error in macOS if file is already there, works if files is created for first time

I think proposed solution

/bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env && echo \"Overwritten .env\"'

should work with all OS and also clearly states that developer should adhere to pre-built env, why are we looking for another one?
Do we need something specific here?

@palisadoes
Copy link
Contributor

This doesn't make sense. All the man pages state that the -n flag works as in Linux

  1. https://ss64.com/mac/cp.html
  2. https://www.unix.com/man-page/osx/1/cp/

Other questions.

  1. What version of MacOS are you running?
  2. Please provide a screen shot of the man page and the -n command
  3. Please provide the output of this command too:
    /bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env'

@JaiPannu-IITI
Copy link
Author

JaiPannu-IITI commented Feb 1, 2025

@palisadoes

  1. I am using MacOS Sequoia V 15.2

2.In Mac:
Image
In Linux:
Image

Difference: **Do Not Fail ** : specified in Linux not in Mac (Different error handling for same command), In Mac throws error, in linux just skips as it is

To generate exact same logic as we currently have In linux, we can also use
/bin/sh -c '[ ! -f .env ] && cp ./envFiles/.env.devcontainer ./.env || echo \".env already exists, skipping copy.\"'
which copies existing file and skips if already there without throwing error

You can also refer these recordings

command.testing.macOS.mov
Command.Testing.Linux.mov
  1. /bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env'
    This command works perfectly, even with existing .env, also when starting container or opening it in vscode.
Image

But it overwrites file in both linux and Mac.

I think we can use either of these
/bin/sh -c '[ ! -f .env ] && cp ./envFiles/.env.devcontainer ./.env || echo \".env already exists, skipping copy.\"' (linux nature)
/bin/sh -c 'cp -f ./envFiles/.env.devcontainer ./.env' (overwrite file)
depending on our use case.

@palisadoes
Copy link
Contributor

  1. We cannot overwrite an existing file for the reasons stated previously. This needs to be the solution.
    /bin/sh -c '[ ! -f .env ] && cp ./envFiles/.env.devcontainer ./.env 
    
  2. Please open a PR with this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unapproved Unapproved for Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants