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

Config: Check current working directory for existing config dir #6322

Merged
merged 1 commit into from
Mar 20, 2024

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Mar 17, 2024

Currently, the location of the configuration directory could only be controlled through the AIIDA_PATH environment variable. Here, the heuristic is updated to also consider the current working directory or any of its parent directories in case AIIDA_PATH is not set. This provides another easy way for users to maintain multiple separate AiiDA instances.

@danielhollas danielhollas self-requested a review March 17, 2024 22:25
Copy link
Collaborator

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very excited for this feature! 🤩 Gave it a quick test locally and seems to work.
Couple minor comments.

docs/source/howto/installation.rst Outdated Show resolved Hide resolved
src/aiida/manage/configuration/settings.py Outdated Show resolved Hide resolved
src/aiida/manage/configuration/settings.py Show resolved Hide resolved
src/aiida/manage/configuration/settings.py Show resolved Hide resolved
@danielhollas
Copy link
Collaborator

Also, please add .aiida/ to .gitignore.

@sphuber sphuber force-pushed the feature/aiida-path-local-directory branch from 75dbc15 to f019dad Compare March 18, 2024 08:31
@sphuber sphuber requested a review from danielhollas March 18, 2024 08:31
@sphuber sphuber force-pushed the feature/aiida-path-local-directory branch from f019dad to f1512b7 Compare March 18, 2024 09:12
Copy link
Collaborator

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG apart from some nits, but I think one or more tests is warranted.

src/aiida/manage/configuration/settings.py Outdated Show resolved Hide resolved
src/aiida/manage/configuration/settings.py Outdated Show resolved Hide resolved
tests/manage/configuration/test_config.py Outdated Show resolved Hide resolved
docs/source/howto/installation.rst Outdated Show resolved Hide resolved
@sphuber sphuber force-pushed the feature/aiida-path-local-directory branch 2 times, most recently from a94dca5 to 658231b Compare March 18, 2024 15:16
@sphuber sphuber requested a review from danielhollas March 18, 2024 15:16
Copy link
Collaborator

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers!

Though given the scope of the change, other people might want to chime in? Unless this change in behaviour was already discussed and agreed elsewhere.

@sphuber
Copy link
Contributor Author

sphuber commented Mar 18, 2024

Thanks @danielhollas . This was more or less discussed offline with @giovannipizzi . Gio, do you want to have a quick look?

@giovannipizzi
Copy link
Member

I think it's best if @GeigerJ2 checks, he did some extensive testing of various configurations/cases and found some issues (before this pr) and can give a first check (also to see if his issues are fixed by this pr)

Copy link
Contributor

@GeigerJ2 GeigerJ2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good to me! Just added a few minor comments.

docs/source/howto/installation.rst Outdated Show resolved Hide resolved
"""
# Loop over all the paths in the ``AIIDA_PATH`` variable to see if any of them contain a configuration folder
for base_dir_path in [path for path in environment_variable.split(':') if path]:
dirpath_config = pathlib.Path(base_dir_path).expanduser()
Copy link
Contributor

@GeigerJ2 GeigerJ2 Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned by @giovannipizzi, I did some testing with AIIDA_PATH before, and found that if '.' is part of the list of paths (e.g. .:'some_path'), things break. While I'd say it's discouraged to set AIIDA_PATH to the cwd via '.', rather than providing the actual path, the following could still provide a safeguard:

    for base_dir_path in [path for path in environment_variable.split(':') if path]:
        if base_dir_path != '.':
            dirpath_config = pathlib.Path(base_dir_path).expanduser()
        else:
            dirpath_config = pathlib.Path.cwd().expanduser()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the exact way in which this broke for you? I cannot reproduce it. For example:

In [2]: get_configuration_directory_from_envvar('.:/tmp')
Out[2]: PosixPath('/tmp/.aiida')

if there is no .aiida folder in the CWD. If there is such a folder, that path is used. So it seems to me like it is working as intended.

That being said, I notice that there is no validation whatsoever for the value in AIIDA_PATH. I wonder if, rather than trying to accommodate for all possible weird cases like ., we should really be validating and raising for invalid AIIDA_PATH values.
Validation could be:

  1. Should be a string
  2. When split on :, all elements should be absolute filepaths (allowing relative paths is for sure going to cause issues)
  3. Each path (omitting the .aiida leaf if present) has to exist. If it doesn't exist, it is ignored.

Now this may be backwards incompatible, but I think if someone is using anything that does not respect these rules, chances are high that things can easily break anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My issue was prior to this PR. I tested it with and without an empty .aiida directory present (as that was the use case), both in the first and second directory that was set via AIIDA_PATH, and using the verdi profile setup core.sqlite_zip command. This was part of the discussion on what is the fastest way to quickly explore an aiida-archive without polluting the main profile (before your actual verdi init PR). In both cases, nested .aiida/.aiida trees were generated, and, consequently, the exception:
FileNotFoundError: [Errno 2] No such file or directory: '.aiida/access/<profile>'
was thrown as the actual location became '.aiida/.aiida/access/<profile>'.

I just tried the same with the updated code, but still getting the same issue, while the pure Python function gives the same output as what you posted. I suppose there might be more things happening when runnnig verdi profile setup core.sqlite_zip?

However, I am also strongly in favor of doing some validation on AIIDA_PATH. Is it OK if I create a commit directly on this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried the same with the updated code, but still getting the same issue, while the pure Python function gives the same output as what you posted. I suppose there might be more things happening when runnnig verdi profile setup core.sqlite_zip?

What did you run exactly? What is the value of AIIDA_PATH, if any? Which .aiida folders exist on your filesystem, and what is the current working directory when you call verdi profile setup core.sqlite_zip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I am also strongly in favor of doing some validation on AIIDA_PATH. Is it OK if I create a commit directly on this PR?

Since it seems to be an unrelated issue to the changes of this PR, I think it would be best to reserve that for a separate PR. Especially since adding validation would potentially be backwards incompatible, it would be best to not mix this here. I think we also need to discuss the coming team meeting if others agree to add the proposed backwards incompatibility.

Copy link
Contributor

@GeigerJ2 GeigerJ2 Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pasting the testing I did prior to this PR here for completeness (can later be moved to a separate PR):


Some notes:

  • AIIDA_PATH was set to '.' from the parent folder in which the '.aiida' directory was supposed to be created (not that it should actually make a difference), and verdi profile setup core.sqlite_zip was run from this folder
  • Manually added '.aiida' folders were always empty; apart from that I only had my global '~/.aiida'
  • Warning = UserWarning: Creating AiiDA configuration folder '.aiida'
  • ❌ = Breaks with FileNotFoundError due to nested '.aiida/.aiida'

  1. AIIDA_PATH='.'
No second path
First path -> '.aiida' present ❌ '.aiida/.aiida' tree in first path
First path -> '.aiida' absent ❌ Warning, then '.aiida/.aiida' tree in first path

  1. AIIDA_PATH='<first_path>'
No second path
First path -> '.aiida' present ✅ '.aiida' created in first path
First path -> '.aiida' absent ✅ Warning, then '.aiida' created in first path

  1. AIIDA_PATH='.:<second_path>'
Second path -> '.aiida' present Second path -> '.aiida' absent
First path -> '.aiida' present ❌ '.aiida/.aiida' tree in first path ❌ '.aiida/.aiida' tree in first path
First path -> '.aiida' absent ✅ '.aiida' created in second path ✅ Warning, then '.aiida' created in second path

  1. AIIDA_PATH='<first_path>:<second_path>'
Second path -> '.aiida' present Second path -> '.aiida' absent
First path -> '.aiida' present ✅ '.aiida' created in first path ✅ '.aiida' created in first path
First path -> '.aiida' absent ✅ '.aiida' created in second path ✅ Warning, then '.aiida' created in second path

So yeah, without '.', things work as expected, but with it, hell breaks loose. You're right - better to keep it separate and discuss it with the rest of the team 🚀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed analysis @GeigerJ2 . I think this shows that validation of AIIDA_PATH is in order 😅 Is there anything else for this PR remaining, or can we merge this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my side, it's fine! Would have approved the merge directly, but not sure if I am ready to wield such power yet :D Thanks for the changes, I think they go in a good direction!

src/aiida/manage/configuration/settings.py Outdated Show resolved Hide resolved
docs/source/howto/installation.rst Outdated Show resolved Hide resolved
Currently, the location of the configuration directory could only be
controlled through the `AIIDA_PATH` environment variable. Here, the
heuristic is updated to also consider the current working directory or
any of its parent directories in case `AIIDA_PATH` is not set. This
provides another easy way for users to maintain multiple separate AiiDA
instances.
@sphuber sphuber force-pushed the feature/aiida-path-local-directory branch from 658231b to 8767ebd Compare March 19, 2024 13:01
@sphuber sphuber merged commit 1059b5f into aiidateam:main Mar 20, 2024
20 checks passed
@sphuber sphuber deleted the feature/aiida-path-local-directory branch March 20, 2024 08:01
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 this pull request may close these issues.

4 participants