Repository navigation
Warn when Durable Functions dependencies allow major upgrades #350
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
Open
andystaples
wants to merge
3
commits into
main
Choose a base branch
from
andystaples-functions-dependency-version-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
azure-functions-durable/azure/durable_functions/internal/dependency_warning.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Best-effort check for Function App dependencies that allow a new major.""" | ||
|
|
||
| import os | ||
| import re | ||
| import shlex | ||
| from importlib.metadata import PackageNotFoundError, version | ||
| from logging import Logger | ||
| from pathlib import Path | ||
|
|
||
| from packaging.markers import UndefinedComparison, UndefinedEnvironmentName | ||
| from packaging.requirements import InvalidRequirement, Requirement | ||
| from packaging.specifiers import SpecifierSet | ||
| from packaging.utils import canonicalize_name | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
| _PACKAGE = "azure-functions-durable" | ||
| _INDEX_OPTIONS_WITH_VALUE = { | ||
| "-i", "-f", "--index-url", "--extra-index-url", "--find-links", | ||
| "--trusted-host", "--only-binary", "--no-binary", | ||
| } | ||
| _INDEX_FLAGS = {"--no-index", "--pre", "--prefer-binary"} | ||
|
|
||
|
|
||
| def _read_specifiers(root: Path, logger: Logger) -> tuple[SpecifierSet, bool] | None: | ||
| specifiers: list[str] = [] | ||
| required = False | ||
| allow_prereleases = False | ||
| active: set[tuple[Path, bool]] = set() | ||
| visited: set[tuple[Path, bool]] = set() | ||
|
|
||
| def read_file(path: Path, constraint: bool) -> bool: | ||
| nonlocal allow_prereleases, required | ||
| try: | ||
| path = path.resolve() | ||
| key = (path, constraint) | ||
| if key in active: | ||
| logger.debug("Cannot check Durable Functions dependencies: cyclic requirements in %s.", path) | ||
| return False | ||
| if key in visited: | ||
| return True | ||
| content = path.read_text(encoding="utf-8-sig") | ||
| except (OSError, UnicodeError) as exc: | ||
| logger.debug("Cannot read Function App requirements from %s: %s", path, exc) | ||
| return False | ||
|
|
||
| active.add(key) | ||
| try: | ||
| for raw_line in content.splitlines(): | ||
| line = re.sub(r"\s+#.*$", "", raw_line).strip() | ||
| if not line or line.startswith("#"): | ||
| continue | ||
| if line.endswith("\\"): | ||
| logger.debug("Cannot check multiline requirements in %s.", path) | ||
| return False | ||
| if line.startswith("-"): | ||
| try: | ||
| parts = shlex.split(line) | ||
| except ValueError: | ||
| logger.debug("Cannot parse a requirements option in %s.", path) | ||
| return False | ||
| option = parts[0] | ||
| argument = None | ||
| if option in ("-r", "--requirement", "-c", "--constraint") and len(parts) == 2: | ||
| argument = parts[1] | ||
| elif option.startswith(("--requirement=", "--constraint=")) and len(parts) == 1: | ||
| option, argument = option.split("=", 1) | ||
| elif option.startswith(("-r", "-c")) and len(option) > 2 and len(parts) == 1: | ||
| option, argument = option[:2], option[2:] | ||
| elif option in _INDEX_FLAGS and len(parts) == 1: | ||
| allow_prereleases = allow_prereleases or option == "--pre" | ||
| continue | ||
| elif ( | ||
| option in _INDEX_OPTIONS_WITH_VALUE and len(parts) == 2) or ( | ||
| option.split("=", 1)[0] in _INDEX_OPTIONS_WITH_VALUE | ||
| and "=" in option and len(parts) == 1): | ||
| continue | ||
| if not argument or "://" in argument or "${" in argument: | ||
| logger.debug("Cannot inspect a requirements option in %s.", path) | ||
| return False | ||
| nested_constraint = option in ("-c", "--constraint") | ||
| if not read_file(path.parent / argument, nested_constraint): | ||
| return False | ||
| continue | ||
| try: | ||
| requirement = Requirement(line) | ||
| except InvalidRequirement: | ||
| logger.debug("Cannot parse a requirement in %s.", path) | ||
| return False | ||
| if canonicalize_name(requirement.name) != _PACKAGE: | ||
| continue | ||
| try: | ||
| if requirement.marker is not None and not requirement.marker.evaluate(): | ||
| continue | ||
| except (UndefinedComparison, UndefinedEnvironmentName, InvalidVersion) as exc: | ||
| logger.debug("Cannot evaluate a requirement marker in %s: %s", path, exc) | ||
| return False | ||
| if requirement.url is not None: | ||
| logger.debug("Cannot determine the Durable Functions version from a URL in %s.", path) | ||
| return False | ||
| required = required or not constraint | ||
| specifiers.append(str(requirement.specifier)) | ||
| finally: | ||
| active.remove(key) | ||
| visited.add(key) | ||
| return True | ||
|
|
||
| if not read_file(root / "requirements.txt", False) or not required: | ||
| return None | ||
| package_specifiers = SpecifierSet(",".join(specifiers)) | ||
| allow_prereleases = allow_prereleases or any(specifier.prereleases for specifier in package_specifiers) | ||
| return package_specifiers, allow_prereleases | ||
|
|
||
|
|
||
| def _allows_later_major( | ||
| specifiers: SpecifierSet, | ||
| installed: Version, | ||
| allow_prereleases: bool = False) -> bool: | ||
| candidates: set[Version] = set() | ||
|
|
||
| def add_release(epoch: int, major: int, minor: int, patch: int) -> None: | ||
| prefix = f"{epoch}!" if epoch else "" | ||
| candidates.add(Version(f"{prefix}{major}.{minor}.{patch}")) | ||
|
|
||
| for major in (installed.major, installed.major + 1, installed.major + 2): | ||
| add_release(installed.epoch, major, 0, 0) | ||
| add_release(installed.epoch, major, 0, 1) | ||
| add_release(installed.epoch, major, 1, 0) | ||
|
|
||
| for specifier in specifiers: | ||
| try: | ||
| bound = Version(specifier.version.removesuffix(".*")) | ||
| except InvalidVersion: | ||
| continue | ||
| candidates.add(bound) | ||
| add_release(bound.epoch, bound.major, bound.minor, bound.micro + 1) | ||
| add_release(bound.epoch, bound.major, bound.minor + 1, 0) | ||
| add_release(bound.epoch, bound.major + 1, 0, 0) | ||
|
andystaples marked this conversation as resolved.
Resolved
|
||
|
|
||
| for candidate in tuple(candidates): | ||
| base = candidate.base_version | ||
| candidates.update(Version(f"{base}{suffix}") for suffix in (".dev0", "a0", "b0", "rc0")) | ||
| if candidate.pre and candidate.pre[1] > 0: | ||
| label, number = candidate.pre | ||
| candidates.add(Version(f"{base}{label}{number - 1}")) | ||
|
|
||
| allowed = [ | ||
| candidate for candidate in candidates | ||
| if specifiers.contains(candidate, prereleases=allow_prereleases) | ||
| ] | ||
| return ( | ||
| any(candidate.major == installed.major for candidate in allowed) | ||
| and any(candidate.major > installed.major and candidate > installed for candidate in allowed) | ||
| ) | ||
|
|
||
|
|
||
| def warn_for_function_directory(function_directory: str, logger: Logger) -> None: | ||
| """Find the Function App root before checking its dependencies.""" | ||
| roots: list[Path] = [] | ||
| configured_root = os.environ.get("AzureWebJobsScriptRoot") | ||
| if configured_root: | ||
| roots.append(Path(configured_root)) | ||
| directory = Path(function_directory) | ||
| roots.extend((directory, *directory.parents)) | ||
| try: | ||
| roots.append(Path.cwd()) | ||
| except OSError as exc: | ||
| logger.debug("Cannot determine the Function App working directory: %s", exc) | ||
| for root in roots: | ||
| if (root / "host.json").is_file(): | ||
| warn_if_unbounded(str(root), logger) | ||
| return | ||
| logger.debug("Cannot find the Function App root for %s.", function_directory) | ||
|
|
||
|
|
||
| def warn_if_unbounded(function_directory: str, logger: Logger) -> None: | ||
| """Warn when the app's requirements demonstrably permit a later major.""" | ||
| root = Path(function_directory) | ||
| try: | ||
| requirements = _read_specifiers(root, logger) | ||
| except RecursionError as exc: | ||
| logger.debug("Cannot check deeply nested Function App requirements: %s", exc) | ||
| return | ||
| if requirements is None: | ||
| return | ||
| specifiers, allow_prereleases = requirements | ||
| try: | ||
| installed = Version(version(_PACKAGE)) | ||
| allows_later_major = _allows_later_major(specifiers, installed, allow_prereleases) | ||
| except (PackageNotFoundError, InvalidVersion) as exc: | ||
| logger.debug("Cannot determine the installed Durable Functions version: %s", exc) | ||
| return | ||
| if allows_later_major: | ||
| next_major = f"{installed.epoch}!" if installed.epoch else "" | ||
| next_major += str(installed.major + 1) | ||
| logger.warning( | ||
| "The Function App requirements allow azure-functions-durable to upgrade beyond major version %d. " | ||
| "Future major releases may include broad, replay-breaking changes. If a deployment installs these " | ||
| "requirements without a lock, an automatic upgrade could cause existing orchestrations to fail " | ||
| "unexpectedly. Pin the major version, for example azure-functions-durable>=%s,<%s, " | ||
| "or deploy using a complete dependency lock.", | ||
| installed.major, installed.public, next_major, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.