.onLoad <- function(libname, pkgname) {
  # Auto-setup Julia environment ONLY during automated testing;  never runs for regular users

  should_auto_setup <- (
    # Condition 1: NOT_CRAN is "true" (testing environment)
    identical(Sys.getenv("NOT_CRAN"), "true") &&

      # Condition 2: Custom environmental variable is "true"
      identical(Sys.getenv("SDBUILDR_INSTALL_JULIA_ENV"), "true")
  )

  if (should_auto_setup) {
    tryCatch(
      {
        # Check existence Manifest.toml file
        manifest_file <- file.path(julia_env_dir(), "Manifest.toml")
        if (!file.exists(manifest_file)) {
          install_julia_env()

          # Close Julia session
          use_julia(stop = TRUE)
        }
      },
      error = function(e) {
        invisible()
      }
    )
  }
}
