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

feat: Check malformed path entries #303

Merged
merged 2 commits into from
May 16, 2024

Conversation

zermelo-wisen
Copy link
Collaborator

@zermelo-wisen zermelo-wisen commented May 12, 2024

Fixes #302

Warning and dropping malformed configuration file package path entries, which cannot be parsed as Python module names: mod1/mod2, mod1\mod2, 123, ., .

@zermelo-wisen zermelo-wisen force-pushed the feat/check-malformed-path-entries branch 2 times, most recently from eb3f71f to e2ab421 Compare May 13, 2024 06:56
Copy link
Collaborator

@apotterri apotterri left a comment

Choose a reason for hiding this comment

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

Thanks for this. Just a couple of minor changes to make, then it'll be good to go.

_appmap/configuration.py Outdated Show resolved Hide resolved
_appmap/configuration.py Outdated Show resolved Hide resolved
has_separator = '/' in path or '\\' in path
raise AppMapPackagePathException(
f"Malformed path value '{path}' in configuration file. "
"Path entries should be module names"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"Path entries should be module names"
"Path entries must be module names"

def _check_path_values(self):
for item in self._config["packages"]:
path: str = item.get("path")
if path and not self._check_path_value(path):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code should also raise an exception if path is None. This case is currently not handled well, and produces an ugly error message.

@zermelo-wisen zermelo-wisen force-pushed the feat/check-malformed-path-entries branch 2 times, most recently from 3f22958 to 6db2a85 Compare May 13, 2024 14:14
@apotterri
Copy link
Collaborator

I gave these changes a try. Because of the way appmap gets imported (using a .pth file), the UX is terrible:

[2024-05-14 06:25:39,785] ERROR _appmap.configuration: Malformed path value '.' in configuration file. Path entries must be module names.
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 227, in _load_config
    self._check_path_values()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 294, in _check_path_values
    raise AppMapInvalidConfigException(
_appmap.configuration.AppMapInvalidConfigException: Malformed path value '.' in configuration file. Path entries must be module names.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 617, in <module>
    main()
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 610, in main
    execsitecustomize()
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 549, in execsitecustomize
    import sitecustomize
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/test/data/fastapi/init/sitecustomize.py", line 1, in <module>
    import appmap
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/appmap/__init__.py", line 9, in <module>
    from _appmap import generation  # noqa: F401
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/__init__.py", line 1, in <module>
    from . import configuration
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 450, in <module>
    initialize()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 445, in initialize
    Config().initialize()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 146, in __init__
    self._load_config()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 230, in _load_config
    sys.exit(1)
SystemExit: 1

So, we need to change this. When an invalid path is detected`, we should just log a warning and ignore it. This needs to happen without raising an exception, and the agent should not exit.

Copy link
Collaborator

@apotterri apotterri left a comment

Choose a reason for hiding this comment

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

@zermelo-wisen zermelo-wisen force-pushed the feat/check-malformed-path-entries branch from 6db2a85 to 8537fff Compare May 14, 2024 12:46
@zermelo-wisen
Copy link
Collaborator Author

I gave these changes a try. Because of the way appmap gets imported (using a .pth file), the UX is terrible:

[2024-05-14 06:25:39,785] ERROR _appmap.configuration: Malformed path value '.' in configuration file. Path entries must be module names.
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 227, in _load_config
    self._check_path_values()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 294, in _check_path_values
    raise AppMapInvalidConfigException(
_appmap.configuration.AppMapInvalidConfigException: Malformed path value '.' in configuration file. Path entries must be module names.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 617, in <module>
    main()
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 610, in main
    execsitecustomize()
  File "/Users/ajp/.asdf/installs/python/3.10.13/lib/python3.10/site.py", line 549, in execsitecustomize
    import sitecustomize
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/test/data/fastapi/init/sitecustomize.py", line 1, in <module>
    import appmap
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/appmap/__init__.py", line 9, in <module>
    from _appmap import generation  # noqa: F401
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/__init__.py", line 1, in <module>
    from . import configuration
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 450, in <module>
    initialize()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 445, in initialize
    Config().initialize()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 146, in __init__
    self._load_config()
  File "/Users/ajp/src/applandinc/worktrees/appmap-python/_appmap/configuration.py", line 230, in _load_config
    sys.exit(1)
SystemExit: 1

So, we need to change this. When an invalid path is detected`, we should just log a warning and ignore it. This needs to happen without raising an exception, and the agent should not exit.

Done

invalid_items.append(item)
continue

if len(invalid_items) > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's going to happen if this eliminates all the path entries?

Copy link
Collaborator Author

@zermelo-wisen zermelo-wisen May 15, 2024

Choose a reason for hiding this comment

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

I think, no package will be instrumented, unless there are some dist entries. We reach this state with at least one warning.

Thinking again, while loading the config file should we also check if:

  1. packages is a yaml sequence and not a scalar,
  2. packages is empty,
  3. every item has path XOR dist key?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, let's not expand the scope of this beyond checking that paths are modules.

However, please add a test that has only invalid paths, and confirm that the resulting Config has a package with an empty list of paths.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@zermelo-wisen zermelo-wisen force-pushed the feat/check-malformed-path-entries branch from 8537fff to 6cbf90e Compare May 16, 2024 05:47
Update Config so it matches the singleton style of Env.
@apotterri apotterri force-pushed the feat/check-malformed-path-entries branch from 6cbf90e to 8f89354 Compare May 16, 2024 10:06
@apotterri apotterri force-pushed the feat/check-malformed-path-entries branch from 8f89354 to f7937ee Compare May 16, 2024 10:24
@apotterri apotterri merged commit 9627d70 into master May 16, 2024
1 check passed
@apotterri apotterri deleted the feat/check-malformed-path-entries branch May 16, 2024 11:51
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.

the agent should give an error for malformed path entries
3 participants