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

Improve error handling when a broken workspace is activated #494

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/ramble/ramble/cmd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,20 @@ def find_workspace(args):
# nothing was set; there's no active environment
if not ws:
return None

# if we get here, env isn't the name of a spack environment; it has
# to be a path to an environment, or there is something wrong.
elif not ramble.workspace.is_workspace_dir(ws):
env_var = ramble.workspace.ramble_workspace_var
raise ramble.workspace.RambleActiveWorkspaceError(
f'The environment variable {env_var} refers to an invalid ramble workspace.'
)

# if we get here, ws isn't the name of a ramble workspace; it has
# to be a path to a workspace, or there is something wrong.
if ramble.workspace.is_workspace_dir(ws):
return ramble.workspace.Workspace(ws)

raise ramble.workspace.RambleWorkspaceError('no workspace in %s' % ws)
raise ramble.workspace.RambleWorkspaceError(
f'No workspace in {ws}'
)


def find_workspace_path(args):
Expand Down
3 changes: 2 additions & 1 deletion lib/ramble/ramble/cmd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def workspace_deactivate(args):
)

if ramble.workspace.active_workspace() is None:
logger.die('No workspace is currently active.')
if ramble.workspace.ramble_workspace_var not in os.environ:
logger.die('No workspace is currently active.')

cmds = ramble.workspace.shell.deactivate_header(args.shell)
env_mods = ramble.workspace.shell.deactivate()
Expand Down
22 changes: 14 additions & 8 deletions lib/ramble/ramble/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,8 @@ def _main(argv=None):
ramble.workspace.shell.activate(ws)
# print the context but delay this exception so that commands like
# `ramble config edit` can still work with a bad workspace.
except ramble.workspace.RambleActiveWorkspaceError as e:
workspace_format_error = e
except ramble.config.ConfigFormatError as e:
e.print_context()
workspace_format_error = e
Expand Down Expand Up @@ -861,18 +863,22 @@ def finish_parse_and_run(parser, cmd_name, workspace_format_error):
# Now that we know what command this is and what its args are, determine
# whether we can continue with a bad workspace and raise if not.
edit_cmds = ["workspace", "config"]
allowed_subcommands = ['edit', 'list']
allowed_subcommands = ['edit', 'list', 'deactivate']

if workspace_format_error:
raise_error = False
if cmd_name.strip() in edit_cmds:
logger.msg(
"Error while reading workspace config. In some cases this can be " +
"avoided by passing `-W` to ramble"
)
raise_error = True
subcommand = getattr(args, "%s_command" % cmd_name, None)
if subcommand in allowed_subcommands:
raise_error = False

if subcommand != "deactivate":
raise_error = True
logger.msg(
"Error while reading workspace config. In some cases this can be " +
"avoided by passing `-W` to ramble or by running\n" +
"`ramble workspace deactivate`"
)
if subcommand in allowed_subcommands:
raise_error = False

if raise_error:
raise workspace_format_error
Expand Down
2 changes: 2 additions & 0 deletions lib/ramble/ramble/workspace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Workspace,
RambleWorkspaceError,
RambleConflictingDefinitionError,
RambleActiveWorkspaceError,
RambleMissingApplicationError,
RambleMissingWorkloadError,
RambleMissingExperimentError,
Expand Down Expand Up @@ -50,6 +51,7 @@
'Workspace',
'RambleWorkspaceError',
'RambleConflictingDefinitionError',
'RambleActiveWorkspaceError',
'RambleMissingApplicationError',
'RambleMissingWorkloadError',
'RambleMissingExperimentError',
Expand Down
4 changes: 4 additions & 0 deletions lib/ramble/ramble/workspace/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,10 @@ class RambleConflictingDefinitionError(RambleWorkspaceError):
"""Error when conflicting software definitions are found"""


class RambleActiveWorkspaceError(RambleWorkspaceError):
"""Error when an invalid workspace is activated"""


class RambleMissingApplicationError(RambleWorkspaceError):
"""Error when using an undefined application in an experiment
specification"""
Expand Down