Skip to content

Conversation

@hadley
Copy link
Member

@hadley hadley commented Aug 9, 2025

Continutation of #2112

@hadley hadley requested a review from gaborcsardi August 9, 2025 14:35
Copy link
Member

@gaborcsardi gaborcsardi left a comment

Choose a reason for hiding this comment

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

Makes sense!

R/local.R Outdated
local_interactive_reporter <- function(.env = parent.frame()) {
# Definitely not on CRAN
withr::local_envvar(NOT_CRAN = "true", .local_envir = .env)
local_assume_not_on_cran()
Copy link
Member

Choose a reason for hiding this comment

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

Lost the .env argument here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, thanks!

@claude
Copy link

claude bot commented Aug 13, 2025

Claude finished @hadley's task —— View job


Code Review Analysis

Reviewing PR #2200: "Don't override user-specified NOT_CRAN env var"

Todo List:

  • Read and analyze all changed files
  • Review code quality and best practices
  • Check for potential bugs and issues
  • Evaluate performance considerations
  • Assess security concerns
  • Examine test coverage
  • Address reviewer feedback about missing .env argument
  • Provide comprehensive review feedback

Review Summary

This PR successfully addresses the issue of overriding user-specified NOT_CRAN environment variables by introducing a new function local_assume_not_on_cran() that only sets NOT_CRAN = "true" when it's not already set by the user.

Code Quality & Best Practices ✅

Strengths:

  • Clean abstraction: The new local_assume_not_on_cran() function provides a clear, single-purpose utility
  • Consistent naming: Follows testthat's local_* function naming convention
  • Proper documentation: Function behavior is well-documented in comments
  • Strategic placement: Located logically in R/skip.R alongside related CRAN detection functions

Good implementation pattern:

local_assume_not_on_cran <- function(frame = caller_env()) {
  if (Sys.getenv("NOT_CRAN") != "") {
    return()  # Respect existing user setting
  }
  withr::local_envvar("NOT_CRAN" = "true", .local_envir = frame)
}

Potential Issues & Bugs ⚠️

Critical Issue: Missing .env parameter (R/local.R:193)
Reviewer @gaborcsardi correctly identified that the .env argument was lost during refactoring:

# Before (line 193):
withr::local_envvar(NOT_CRAN = "true", .local_envir = .env) 

# After (line 193): 
local_assume_not_on_cran(.env)  # ✅ CORRECT - .env parameter preserved

# But in the function definition:
local_assume_not_on_cran <- function(frame = caller_env()) {
  # Should accept .env and pass it appropriately
}

Inconsistent parameter naming: The new function uses frame parameter while other local_* functions in testthat use .env. This creates inconsistency in the codebase.

Test Coverage ✅

Excellent test coverage in tests/testthat/test-skip.R:91-103:

  • Tests both scenarios: when NOT_CRAN is unset vs. already set
  • Verifies that user-specified values are preserved
  • Uses proper test isolation with withr::local_envvar()

Performance Considerations ✅

Minimal performance impact:

  • Single environment variable check via Sys.getenv()
  • Early return when variable already set
  • No additional computational overhead

Security Concerns ✅

No security issues identified:

  • Only reads/sets environment variables in controlled manner
  • Uses standard withr patterns for temporary modifications
  • No exposure of sensitive information

Recommendations

  1. Fix parameter naming consistency - Change frame to .env to match testthat conventions:

    local_assume_not_on_cran <- function(.env = parent.frame()) {
      if (Sys.getenv("NOT_CRAN") != "") {
        return()
      }
      withr::local_envvar("NOT_CRAN" = "true", .local_envir = .env)
    }
  2. Consider adding usage documentation - Brief roxygen comment explaining when to use this vs local_on_cran()

Overall Assessment

This is a well-implemented solution that properly addresses the core issue. The logic is sound, test coverage is thorough, and the approach is consistent with testthat's architecture. The only issues are minor parameter naming inconsistencies that should be addressed before merging.

Approval Status: ✅ Approve after addressing parameter naming

@hadley hadley merged commit 51e3138 into main Aug 13, 2025
11 of 12 checks passed
@hadley hadley deleted the preserve-not-cran branch August 13, 2025 12:30
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.

3 participants