Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 15, 2025

Summary

This PR enhances Nerdbank.GitVersioning to automatically disable the git engine when running under Dependabot, eliminating the need for manual configuration workarounds.

Problem

Dependabot operates on shallow clones, which causes Nerdbank.GitVersioning to fail since it requires access to git history. Previously, users needed to work around this limitation manually since Dependabot doesn't support custom environment variables. While a workaround existed (see dependabot/dependabot-core#4660), it required extra configuration that most users shouldn't need to manage.

Solution

This PR implements automatic detection of Dependabot environments:

  • When the DEPENDABOT environment variable is set to "true" (case-insensitive) and the NBGV_GitEngine environment variable is not set, Nerdbank.GitVersioning automatically behaves as if NBGV_GitEngine=Disabled
  • Users can still explicitly set NBGV_GitEngine to override this automatic behavior if needed
  • The feature works seamlessly across all integration points: MSBuild tasks, the nbgv CLI tool, and cloud build commands

Implementation Details

  1. Core Logic (GitContext.cs):

    • Added GetEffectiveGitEngine() public static method that centralizes engine selection logic
    • Checks for Dependabot environment via the DEPENDABOT environment variable
    • Parses NBGV_GitEngine values (LibGit2, Managed, Disabled) in one location
    • Always respects explicit NBGV_GitEngine settings (maintains user control)
  2. Integration Points:

    • MSBuild (GetBuildVersion.cs): MSBuild GitEngine property takes highest precedence, followed by environment variables
    • CLI Tool (Program.cs): Updated nbgv commands to use the centralized logic
    • Cloud Command (CloudCommand.cs): Updated cloud build integration
  3. Documentation (shallow-cloning-agents.md):

    • Updated Dependabot section to explain automatic behavior
    • Clarified that explicit NBGV_GitEngine setting overrides automatic detection
    • Noted that the previous workaround is no longer necessary for most users

Testing

  • Added 22 comprehensive unit tests covering:
    • Default behavior with no environment variables
    • Automatic disabling when DEPENDABOT=true
    • Case-insensitive handling of DEPENDABOT values
    • Override behavior when NBGV_GitEngine is explicitly set
    • All valid NBGV_GitEngine values (LibGit2, Managed, Disabled)
  • All 1,105 tests pass
  • Manual testing confirms correct behavior across various environment configurations

Backward Compatibility

This change is fully backward compatible:

  • Existing NBGV_GitEngine environment variable always takes precedence
  • MSBuild GitEngine property continues to override environment variables
  • Only affects Dependabot environments where NBGV_GitEngine is not set
  • Invalid NBGV_GitEngine values are silently ignored (maintains existing behavior)
  • No breaking changes to public APIs

Example Usage

# Dependabot automatically gets Engine.Disabled
export DEPENDABOT=true
# No NBGV_GitEngine set - engine is automatically disabled

# Users can still override if needed
export DEPENDABOT=true
export NBGV_GitEngine=LibGit2  # This takes precedence

Fixes #[issue-number]

Original prompt

Problem

Enhance Nerdbank.GitVersioning to automatically deactivate the git engine when running under Dependabot. Specifically:

  • When the DEPENDABOT environment variable is set to true (case-insensitive), and the NBGV_GitEngine environment variable is not set, the tool should automatically behave as if NBGV_GitEngine is set to Disabled.
  • This should effectively disable the git engine for Dependabot runs unless explicitly overridden by the user.

Tasks

  1. Update the codebase so that on startup, if DEPENDABOT=true (case-insensitive) and NBGV_GitEngine is unset, the git engine is considered disabled.
  2. Update the documentation at docfx\docs\shallow-cloning-agents.md to explain this behavior so users are aware this is now automatic for Dependabot.

Acceptance Criteria

  • The git engine is disabled automatically in Dependabot environments unless overridden.
  • Documentation clearly describes this logic and its rationale.

This PR implements the above logic and documentation update.

This pull request was created as a result of the following prompt from Copilot chat.

Problem

Enhance Nerdbank.GitVersioning to automatically deactivate the git engine when running under Dependabot. Specifically:

  • When the DEPENDABOT environment variable is set to true (case-insensitive), and the NBGV_GitEngine environment variable is not set, the tool should automatically behave as if NBGV_GitEngine is set to Disabled.
  • This should effectively disable the git engine for Dependabot runs unless explicitly overridden by the user.

Tasks

  1. Update the codebase so that on startup, if DEPENDABOT=true (case-insensitive) and NBGV_GitEngine is unset, the git engine is considered disabled.
  2. Update the documentation at docfx\docs\shallow-cloning-agents.md to explain this behavior so users are aware this is now automatic for Dependabot.

Acceptance Criteria

  • The git engine is disabled automatically in Dependabot environments unless overridden.
  • Documentation clearly describes this logic and its rationale.

This PR implements the above logic and documentation update.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Oct 15, 2025
@AArnott
Copy link
Collaborator

AArnott commented Oct 15, 2025

@copilot You can find additional context on the problem being fixed here at dependabot/dependabot-core#4660

Copilot AI and others added 3 commits October 15, 2025 13:06
- Add GetEffectiveGitEngine method to GitContext that checks DEPENDABOT env var
- Update GetBuildVersion task to use the new logic
- Update nbgv CLI tool (Program.cs) to use the new logic
- Update CloudCommand to use the new logic
- Add comprehensive tests for Dependabot detection
- Update documentation to explain the automatic behavior

Co-authored-by: AArnott <[email protected]>
- Move NBGV_GitEngine parsing into GitContext.GetEffectiveGitEngine
- Simplify Program.cs GetEffectiveGitEngine to just call GitContext method
- Update GetBuildVersion to respect MSBuild property over env vars
- Enhance tests to cover all NBGV_GitEngine values (LibGit2, Managed, Disabled)
- All 1105 tests passing

Co-authored-by: AArnott <[email protected]>
- Clarify that unrecognized NBGV_GitEngine values are silently ignored
- Document that this maintains backward compatibility
- Explain valid values are LibGit2, Managed, and Disabled (case-sensitive)

Co-authored-by: AArnott <[email protected]>
Copilot AI changed the title [WIP] Enhance git versioning for Dependabot integration Automatically disable git engine for Dependabot environments Oct 15, 2025
Copilot AI requested a review from AArnott October 15, 2025 13:22
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.

2 participants