dd-trace 5.134.0 → 5.136.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (353) hide show
  1. package/LICENSE-3rdparty.csv +0 -1
  2. package/ci/cypress/support.js +15 -1
  3. package/ci/init.js +2 -22
  4. package/ci/test-annotations.d.ts +5 -0
  5. package/ci/test-annotations.js +5 -0
  6. package/ci/vitest-no-worker-init-annotations.mjs +4 -0
  7. package/ci/vitest-no-worker-init-node-setup.mjs +22 -0
  8. package/ci/vitest-no-worker-init-runner-setup.mjs +7 -32
  9. package/ci/vitest-no-worker-init-setup.mjs +34 -12
  10. package/ci/vitest-no-worker-init-v5-setup.mjs +6 -0
  11. package/ext/exporters.js +1 -1
  12. package/index.d.ts +124 -5
  13. package/initialize.mjs +5 -2
  14. package/lambda.d.ts +39 -0
  15. package/lambda.js +3 -0
  16. package/loader-hook.mjs +53 -0
  17. package/openfeature/exposure-hook.d.ts +66 -0
  18. package/openfeature/exposure-hook.js +5 -0
  19. package/package.json +19 -15
  20. package/packages/datadog-core/src/storage.js +1 -1
  21. package/packages/datadog-esbuild/index.js +78 -62
  22. package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
  23. package/packages/datadog-instrumentations/src/cucumber.js +56 -19
  24. package/packages/datadog-instrumentations/src/cypress-config.js +163 -15
  25. package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
  26. package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
  27. package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
  28. package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
  29. package/packages/datadog-instrumentations/src/graphql.js +29 -20
  30. package/packages/datadog-instrumentations/src/grpc/server.js +23 -6
  31. package/packages/{datadog-esbuild/src/utils.js → datadog-instrumentations/src/helpers/bundler-esm-exports.js} +282 -40
  32. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +573 -0
  33. package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
  34. package/packages/datadog-instrumentations/src/helpers/fetch.js +5 -1
  35. package/packages/datadog-instrumentations/src/helpers/graphql-jit-runtime.js +30 -8
  36. package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
  37. package/packages/datadog-instrumentations/src/helpers/hooks.js +4 -8
  38. package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
  39. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +18 -6
  40. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +26 -1
  41. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
  42. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +3 -1
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +40 -0
  46. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
  47. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/react-router.js +49 -0
  48. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
  49. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/typesafe.js +23 -0
  50. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/webdriverio.js +12 -0
  51. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +58 -0
  52. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.js +19 -2
  53. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +25 -3
  54. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
  55. package/packages/datadog-instrumentations/src/ioredis.js +101 -3
  56. package/packages/datadog-instrumentations/src/jest.js +265 -82
  57. package/packages/datadog-instrumentations/src/mariadb-bundle.js +69 -11
  58. package/packages/datadog-instrumentations/src/mariadb.js +13 -6
  59. package/packages/datadog-instrumentations/src/mocha/common.js +89 -8
  60. package/packages/datadog-instrumentations/src/mocha/main.js +11 -2
  61. package/packages/datadog-instrumentations/src/mocha/utils.js +58 -12
  62. package/packages/datadog-instrumentations/src/mocha/worker.js +1 -1
  63. package/packages/datadog-instrumentations/src/next.js +19 -6
  64. package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
  65. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
  66. package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
  67. package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
  68. package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
  69. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
  70. package/packages/datadog-instrumentations/src/oracledb.js +33 -0
  71. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
  72. package/packages/datadog-instrumentations/src/pg.js +2 -1
  73. package/packages/datadog-instrumentations/src/playwright.js +81 -28
  74. package/packages/datadog-instrumentations/src/react-router.js +106 -0
  75. package/packages/datadog-instrumentations/src/typesafe.js +89 -0
  76. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +84 -48
  77. package/packages/datadog-instrumentations/src/vitest-main.js +26 -0
  78. package/packages/datadog-instrumentations/src/vitest-test-annotations.js +20 -0
  79. package/packages/datadog-instrumentations/src/vitest-worker.js +39 -17
  80. package/packages/datadog-instrumentations/src/vitest.js +10 -8
  81. package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
  82. package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
  83. package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
  84. package/packages/datadog-plugin-cucumber/src/index.js +5 -0
  85. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +296 -55
  86. package/packages/datadog-plugin-cypress/src/index.js +1 -1
  87. package/packages/datadog-plugin-cypress/src/lifecycle-owner.js +20 -0
  88. package/packages/datadog-plugin-cypress/src/plugin.js +10 -0
  89. package/packages/datadog-plugin-cypress/src/retry-config-observer.js +281 -0
  90. package/packages/datadog-plugin-cypress/src/setup-handoff.js +125 -0
  91. package/packages/datadog-plugin-cypress/src/support.js +1217 -439
  92. package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
  93. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -7
  94. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +19 -52
  95. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +4 -2
  96. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
  97. package/packages/datadog-plugin-graphql/src/execute.js +2 -2
  98. package/packages/datadog-plugin-graphql/src/utils.js +5 -5
  99. package/packages/datadog-plugin-http/src/client.js +32 -9
  100. package/packages/datadog-plugin-http2/src/client.js +38 -13
  101. package/packages/datadog-plugin-jest/src/index.js +25 -3
  102. package/packages/datadog-plugin-mocha/src/index.js +47 -6
  103. package/packages/datadog-plugin-next/src/index.js +112 -14
  104. package/packages/datadog-plugin-oracledb/src/connection-parser.js +21 -11
  105. package/packages/datadog-plugin-playwright/src/index.js +4 -0
  106. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
  107. package/packages/datadog-plugin-react-router/src/index.js +118 -0
  108. package/packages/datadog-plugin-trpc/src/index.js +15 -0
  109. package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
  110. package/packages/datadog-plugin-trpc/src/request.js +81 -0
  111. package/packages/datadog-plugin-typesafe/src/index.js +17 -0
  112. package/packages/datadog-plugin-typesafe/src/tracing.js +31 -0
  113. package/packages/datadog-plugin-undici/src/index.js +22 -7
  114. package/packages/datadog-plugin-vitest/src/index.js +8 -0
  115. package/packages/datadog-turbopack/index.js +4 -28
  116. package/packages/datadog-turbopack/src/loader.js +14 -452
  117. package/packages/datadog-webpack/index.js +3 -55
  118. package/packages/dd-trace/src/agent/info.js +2 -1
  119. package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
  120. package/packages/dd-trace/src/aiguard/sdk.js +8 -13
  121. package/packages/dd-trace/src/aiguard/tags.js +2 -0
  122. package/packages/dd-trace/src/appsec/blocking/index.js +4 -0
  123. package/packages/dd-trace/src/appsec/graphql.js +91 -6
  124. package/packages/dd-trace/src/appsec/recommended.json +1287 -90
  125. package/packages/dd-trace/src/before-exit.js +32 -0
  126. package/packages/dd-trace/src/bootstrap.js +5 -3
  127. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +24 -18
  128. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +16 -36
  129. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
  130. package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
  131. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +10 -18
  132. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +10 -15
  133. package/packages/dd-trace/src/ci-visibility/requests/route.js +44 -0
  134. package/packages/dd-trace/src/ci-visibility/telemetry-logs.js +5 -0
  135. package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
  136. package/packages/dd-trace/src/ci-visibility/test-annotations.js +72 -0
  137. package/packages/dd-trace/src/ci-visibility/test-declaration-annotations.js +170 -0
  138. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +10 -17
  139. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
  140. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
  141. package/packages/dd-trace/src/config/config-types.d.ts +3 -0
  142. package/packages/dd-trace/src/config/defaults.js +9 -3
  143. package/packages/dd-trace/src/config/generated-config-types.d.ts +46 -2
  144. package/packages/dd-trace/src/config/index.js +111 -43
  145. package/packages/dd-trace/src/config/parsers.js +18 -1
  146. package/packages/dd-trace/src/config/supported-configurations.json +174 -4
  147. package/packages/dd-trace/src/constants.js +1 -0
  148. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +2 -1
  149. package/packages/dd-trace/src/debugger/probe_sampler.js +6 -6
  150. package/packages/dd-trace/src/encode/0.4.js +2 -2
  151. package/packages/dd-trace/src/esm-module-load.js +58 -0
  152. package/packages/dd-trace/src/exporter.js +23 -15
  153. package/packages/dd-trace/src/exporters/agent/index.js +3 -1
  154. package/packages/dd-trace/src/exporters/agentless/index.js +3 -1
  155. package/packages/dd-trace/src/exporters/common/request.js +83 -50
  156. package/packages/dd-trace/src/exporters/common/retry.js +12 -7
  157. package/packages/dd-trace/src/exporters/common/url.js +1 -1
  158. package/packages/dd-trace/src/exporters/llmobs/index.js +135 -0
  159. package/packages/dd-trace/src/exporters/log/index.js +4 -3
  160. package/packages/dd-trace/src/id.js +1 -1
  161. package/packages/dd-trace/src/lambda/facade.js +74 -0
  162. package/packages/dd-trace/src/lambda/handler.js +3 -41
  163. package/packages/dd-trace/src/lambda/index.js +38 -15
  164. package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
  165. package/packages/dd-trace/src/lambda/wrap.js +44 -0
  166. package/packages/dd-trace/src/llmobs/constants/tags.js +3 -0
  167. package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
  168. package/packages/dd-trace/src/llmobs/experiments/index.js +2 -3
  169. package/packages/dd-trace/src/llmobs/experiments/noop.js +5 -2
  170. package/packages/dd-trace/src/llmobs/index.js +13 -2
  171. package/packages/dd-trace/src/llmobs/plugins/claude-agent-sdk/index.js +1 -1
  172. package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
  173. package/packages/dd-trace/src/llmobs/plugins/genai/index.js +1 -1
  174. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +27 -10
  175. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
  176. package/packages/dd-trace/src/llmobs/plugins/typesafe/index.js +102 -0
  177. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
  178. package/packages/dd-trace/src/llmobs/prompts/cache.js +3 -1
  179. package/packages/dd-trace/src/llmobs/sdk.js +21 -1
  180. package/packages/dd-trace/src/llmobs/span_processor.js +219 -12
  181. package/packages/dd-trace/src/llmobs/tagger.js +25 -0
  182. package/packages/dd-trace/src/llmobs/writers/util.js +3 -3
  183. package/packages/dd-trace/src/log/channels.js +4 -0
  184. package/packages/dd-trace/src/log/writer.js +13 -2
  185. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +68 -6
  186. package/packages/dd-trace/src/openfeature/configuration_source.js +41 -17
  187. package/packages/dd-trace/src/openfeature/debug-logging-hook.js +66 -3
  188. package/packages/dd-trace/src/openfeature/exposure-hook.js +185 -0
  189. package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
  190. package/packages/dd-trace/src/openfeature/flagging_provider.js +58 -14
  191. package/packages/dd-trace/src/openfeature/remote_config.js +16 -3
  192. package/packages/dd-trace/src/openfeature/writers/base.js +63 -30
  193. package/packages/dd-trace/src/openfeature/writers/exposures.js +161 -1
  194. package/packages/dd-trace/src/openfeature/writers/flag-eval-evp-hook.js +5 -0
  195. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-aggregation.js +9 -4
  196. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-payload.js +2 -0
  197. package/packages/dd-trace/src/openfeature/writers/flag-evaluation-pii.js +2 -0
  198. package/packages/dd-trace/src/openfeature/writers/split-features.js +25 -0
  199. package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
  200. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
  201. package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
  202. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
  203. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
  204. package/packages/dd-trace/src/opentelemetry/metrics/periodic_metric_reader.js +2 -2
  205. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
  206. package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
  207. package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
  208. package/packages/dd-trace/src/opentelemetry/span.js +2 -2
  209. package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
  210. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
  211. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
  212. package/packages/dd-trace/src/opentelemetry/tracer_provider.js +5 -1
  213. package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
  214. package/packages/dd-trace/src/opentracing/tracer.js +33 -13
  215. package/packages/dd-trace/src/otel-thread-ctx.js +72 -49
  216. package/packages/dd-trace/src/plugin_manager.js +34 -7
  217. package/packages/dd-trace/src/plugins/ci_plugin.js +45 -14
  218. package/packages/dd-trace/src/plugins/index.js +9 -0
  219. package/packages/dd-trace/src/plugins/plugin.js +4 -2
  220. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  221. package/packages/dd-trace/src/plugins/util/ci.js +18 -1
  222. package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
  223. package/packages/dd-trace/src/plugins/util/git.js +273 -86
  224. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +77 -50
  225. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
  226. package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
  227. package/packages/dd-trace/src/plugins/util/tags.js +6 -0
  228. package/packages/dd-trace/src/plugins/util/test.js +90 -45
  229. package/packages/dd-trace/src/plugins/util/url.js +10 -2
  230. package/packages/dd-trace/src/plugins/util/user-provided-git.js +2 -2
  231. package/packages/dd-trace/src/plugins/util/web.js +81 -10
  232. package/packages/dd-trace/src/process-tags/index.js +1 -1
  233. package/packages/dd-trace/src/profiling/config.js +6 -3
  234. package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
  235. package/packages/dd-trace/src/proxy.js +48 -57
  236. package/packages/dd-trace/src/serverless.js +3 -2
  237. package/packages/dd-trace/src/span_processor.js +41 -9
  238. package/packages/dd-trace/src/span_stats.js +28 -2
  239. package/packages/dd-trace/src/startup-log.js +8 -5
  240. package/packages/dd-trace/src/tagger.js +7 -5
  241. package/packages/dd-trace/src/telemetry/dependencies.js +4 -0
  242. package/packages/dd-trace/src/telemetry/index.js +3 -0
  243. package/packages/dd-trace/src/telemetry/logs/index.js +21 -2
  244. package/packages/dd-trace/src/telemetry/logs/log-collector.js +1 -0
  245. package/packages/dd-trace/src/telemetry/metrics.js +2 -1
  246. package/packages/dd-trace/src/telemetry/telemetry.js +10 -3
  247. package/packages/dd-trace/src/tracer.js +0 -2
  248. package/packages/dd-trace/src/util.js +1 -1
  249. package/packages/dd-trace/src/web-tags-cache.js +8 -9
  250. package/register.js +4 -2
  251. package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
  252. package/vendor/dist/{@opentelemetry/resources → @datadog/flagging-core}/LICENSE +22 -2
  253. package/vendor/dist/@datadog/flagging-core/index.js +1 -0
  254. package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
  255. package/vendor/dist/@datadog/openfeature-node-server/index.js +1 -1
  256. package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
  257. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.d.ts +78 -0
  258. package/vendor/dist/@datadog/openfeature-node-server/rules-based/index.js +1 -1
  259. package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
  260. package/vendor/dist/@datadog/source-map/LICENSE +1 -0
  261. package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
  262. package/vendor/dist/@opentelemetry/core/LICENSE +209 -1
  263. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  264. package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
  265. package/vendor/dist/escape-string-regexp/LICENSE +13 -0
  266. package/vendor/dist/esquery/LICENSE +2 -0
  267. package/vendor/dist/https-proxy-agent/LICENSE +80 -1
  268. package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
  269. package/vendor/dist/jest-docblock/LICENSE +16 -0
  270. package/vendor/dist/jsonpath-plus/LICENSE +13 -0
  271. package/vendor/dist/limiter/LICENSE +2 -0
  272. package/vendor/dist/lodash.sortby/LICENSE +2 -0
  273. package/vendor/dist/lru-cache/LICENSE +13 -0
  274. package/vendor/dist/meriyah/LICENSE +13 -0
  275. package/vendor/dist/module-details-from-path/LICENSE +2 -0
  276. package/vendor/dist/mutexify/promise/LICENSE +28 -0
  277. package/vendor/dist/pprof-format/LICENSE +13 -0
  278. package/vendor/dist/pprof-format/index.js +1 -1
  279. package/vendor/dist/protobufjs/LICENSE +219 -0
  280. package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
  281. package/vendor/dist/proxy-from-env/LICENSE +13 -0
  282. package/vendor/dist/retry/LICENSE +2 -0
  283. package/vendor/dist/rfdc/LICENSE +2 -0
  284. package/vendor/dist/semifies/LICENSE +3 -1
  285. package/vendor/dist/shell-quote/LICENSE +3 -1
  286. package/vendor/dist/source-map/LICENSE +1 -0
  287. package/vendor/dist/source-map/lib/util/LICENSE +1 -0
  288. package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
  289. package/vendor/dist/ttl-set/LICENSE +28 -0
  290. package/ci/diagnose.js +0 -2215
  291. package/ci/runbook.md +0 -299
  292. package/ci/test-optimization-validation/approval-artifacts.js +0 -175
  293. package/ci/test-optimization-validation/approval.js +0 -386
  294. package/ci/test-optimization-validation/artifact-id.js +0 -19
  295. package/ci/test-optimization-validation/blocker-category.js +0 -24
  296. package/ci/test-optimization-validation/bounded-json.js +0 -120
  297. package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
  298. package/ci/test-optimization-validation/ci-discovery.js +0 -192
  299. package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
  300. package/ci/test-optimization-validation/ci-remediation.js +0 -283
  301. package/ci/test-optimization-validation/cli.js +0 -954
  302. package/ci/test-optimization-validation/command-blocker.js +0 -377
  303. package/ci/test-optimization-validation/command-output-policy.js +0 -188
  304. package/ci/test-optimization-validation/command-runner.js +0 -650
  305. package/ci/test-optimization-validation/environment.js +0 -88
  306. package/ci/test-optimization-validation/executable-approval.js +0 -47
  307. package/ci/test-optimization-validation/executable.js +0 -244
  308. package/ci/test-optimization-validation/execution-lock.js +0 -70
  309. package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
  310. package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
  311. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
  312. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
  313. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
  314. package/ci/test-optimization-validation/generated-file-policy.js +0 -63
  315. package/ci/test-optimization-validation/generated-files.js +0 -425
  316. package/ci/test-optimization-validation/generated-test-contract.js +0 -284
  317. package/ci/test-optimization-validation/generated-verifier.js +0 -235
  318. package/ci/test-optimization-validation/literal-environment.js +0 -56
  319. package/ci/test-optimization-validation/literal-glob.js +0 -52
  320. package/ci/test-optimization-validation/manifest-loader.js +0 -116
  321. package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
  322. package/ci/test-optimization-validation/manifest-schema.js +0 -695
  323. package/ci/test-optimization-validation/offline-fixtures.js +0 -335
  324. package/ci/test-optimization-validation/offline-output.js +0 -406
  325. package/ci/test-optimization-validation/package-check.js +0 -97
  326. package/ci/test-optimization-validation/payload-normalizer.js +0 -72
  327. package/ci/test-optimization-validation/plan-writer.js +0 -541
  328. package/ci/test-optimization-validation/preflight-runner.js +0 -186
  329. package/ci/test-optimization-validation/project-build-artifact.js +0 -31
  330. package/ci/test-optimization-validation/redaction.js +0 -319
  331. package/ci/test-optimization-validation/report-writer.js +0 -666
  332. package/ci/test-optimization-validation/result-semantics.js +0 -103
  333. package/ci/test-optimization-validation/runner-command.js +0 -264
  334. package/ci/test-optimization-validation/runner-contract.js +0 -695
  335. package/ci/test-optimization-validation/safe-files.js +0 -202
  336. package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
  337. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
  338. package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
  339. package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
  340. package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
  341. package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
  342. package/ci/test-optimization-validation/source-text.js +0 -84
  343. package/ci/test-optimization-validation/static-diagnosis.js +0 -166
  344. package/ci/test-optimization-validation/test-output.js +0 -120
  345. package/ci/test-optimization-validation/validation-blocker.js +0 -21
  346. package/ci/validate-test-optimization.js +0 -3
  347. package/packages/datadog-esbuild/src/resolver.js +0 -284
  348. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
  349. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
  350. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
  351. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
  352. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
  353. package/vendor/dist/@opentelemetry/resources/index.js +0 -1
@@ -20,6 +20,8 @@ const {
20
20
  GIT_COMMIT_SHA,
21
21
  GIT_BRANCH,
22
22
  GIT_REPOSITORY_URL,
23
+ GIT_CHECKOUT_ROOT,
24
+ GIT_CHECKOUT_IS_CUSTOM,
23
25
  GIT_TAG,
24
26
  GIT_COMMIT_MESSAGE,
25
27
  GIT_COMMIT_COMMITTER_DATE,
@@ -39,6 +41,7 @@ const {
39
41
  } = require('./tags')
40
42
  const { filterSensitiveInfoFromRepository } = require('./url')
41
43
  const { cachedExec } = require('./git-cache')
44
+ const { validateGitCommitSha } = require('./user-provided-git')
42
45
 
43
46
  /** @typedef {NonNullable<ReturnType<string['match']>>} RegExpMatch */
44
47
 
@@ -46,6 +49,18 @@ const legacyStorage = storage('legacy')
46
49
 
47
50
  const GIT_REV_LIST_MAX_BUFFER = 12 * 1024 * 1024 // 12MB
48
51
 
52
+ function getGitEnvironment () {
53
+ const env = { ...getEnvironmentVariables() }
54
+ // Git resolves repository locations before moving from a project subdirectory to the worktree root.
55
+ for (const key of ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_COMMON_DIR']) {
56
+ if (env[key] && !path.isAbsolute(env[key])) {
57
+ // Keep POSIX symlink/.. traversal; Windows also supports drive-relative paths.
58
+ env[key] = path.sep === '\\' ? path.resolve(env[key]) : `${process.cwd()}${path.sep}${env[key]}`
59
+ }
60
+ }
61
+ return env
62
+ }
63
+
49
64
  function sanitizedExec (
50
65
  flags,
51
66
  operationMetric,
@@ -65,10 +80,12 @@ function sanitizedExec (
65
80
  try {
66
81
  const exec = executionOptions?.uncached ? execFileSync : cachedExec
67
82
  /** @type {import('node:child_process').ExecFileSyncOptions} */
68
- const options = { stdio: 'pipe' }
83
+ const options = { stdio: 'pipe', env: getGitEnvironment() }
84
+ if (executionOptions?.cwd) options.cwd = executionOptions.cwd
69
85
  if (executionOptions?.timeout) options.timeout = executionOptions.timeout
70
86
  if (executionOptions?.localOnly) {
71
- options.env = { ...getEnvironmentVariables(), GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' }
87
+ options.env.GIT_NO_LAZY_FETCH = '1'
88
+ options.env.GIT_ALLOW_PROTOCOL = ''
72
89
  }
73
90
  let result = exec('git', flags, options).toString()
74
91
 
@@ -88,7 +105,11 @@ function sanitizedExec (
88
105
  exitCode: err.status || err.errno,
89
106
  })
90
107
  }
91
- log.error('Git plugin error executing command', err)
108
+ if (executionOptions?.expectedFailure) {
109
+ log.debug('Git plugin error executing command', err)
110
+ } else {
111
+ log.error('Git plugin error executing command', err)
112
+ }
92
113
  if (executionOptions?.throwOnError) throw err
93
114
  return ''
94
115
  }
@@ -114,19 +135,19 @@ function isGitAvailable () {
114
135
  }
115
136
  }
116
137
 
117
- function isShallowRepository (uncached = false) {
138
+ function isShallowRepository (uncached = false, cwd) {
118
139
  return sanitizedExec(
119
140
  ['rev-parse', '--is-shallow-repository'],
120
141
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'check_shallow' } },
121
142
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'check_shallow' } },
122
143
  { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'check_shallow' } },
123
144
  true,
124
- { uncached }
145
+ { uncached, cwd }
125
146
  ) === 'true'
126
147
  }
127
148
 
128
- function getGitVersion () {
129
- const gitVersionString = sanitizedExec(['version'])
149
+ function getGitVersion (cwd) {
150
+ const gitVersionString = sanitizedExec(['version'], null, null, null, true, { cwd })
130
151
  const gitVersionMatches = /** @type {RegExpMatch} */ (gitVersionString.match(/git version (\d+)\.(\d+)\.(\d+)/))
131
152
  try {
132
153
  return {
@@ -139,8 +160,9 @@ function getGitVersion () {
139
160
  }
140
161
  }
141
162
 
142
- function unshallowRepository (parentOnly = false) {
143
- const gitVersion = getGitVersion()
163
+ function unshallowRepository (parentOnly = false, commitSha, cwd) {
164
+ if (commitSha && !validateGitCommitSha(commitSha)) return
165
+ const gitVersion = getGitVersion(cwd)
144
166
  if (!gitVersion) {
145
167
  log.warn('Git version could not be extracted, so git unshallow will not proceed')
146
168
  return
@@ -149,12 +171,19 @@ function unshallowRepository (parentOnly = false) {
149
171
  log.warn('Git version is <2.27, so git unshallow will not proceed')
150
172
  return
151
173
  }
152
- const defaultRemoteName = sanitizedExec(['config', '--default', 'origin', '--get', 'clone.defaultRemoteName'])
153
- const revParseHead = sanitizedExec(['rev-parse', 'HEAD'])
174
+ const defaultRemoteName = getGitRemoteName(cwd)
175
+ const checkoutHead = sanitizedExec(['rev-parse', 'HEAD'], null, null, null, true, { uncached: true, cwd })
176
+ const revParseHead = commitSha || checkoutHead
177
+ const targetedRecovery = commitSha && commitSha.toLowerCase() !== checkoutHead.toLowerCase()
178
+ /** @type {import('node:child_process').ExecFileSyncOptionsWithBufferEncoding} */
179
+ const fetchOptions = { stdio: 'pipe', env: getGitEnvironment() }
180
+ if (cwd) fetchOptions.cwd = cwd
181
+ if (targetedRecovery) fetchOptions.timeout = 5000
182
+ const depth = parentOnly ? '--deepen=1' : '--shallow-since="1 month ago"'
154
183
 
155
184
  const baseGitOptions = [
156
185
  'fetch',
157
- parentOnly ? '--deepen=1' : '--shallow-since="1 month ago"',
186
+ depth,
158
187
  '--update-shallow',
159
188
  '--filter=blob:none',
160
189
  '--recurse-submodules=no',
@@ -168,7 +197,7 @@ function unshallowRepository (parentOnly = false) {
168
197
  revParseHead,
169
198
  ]
170
199
  try {
171
- cachedExec('git', flags)
200
+ execFileSync('git', flags, fetchOptions)
172
201
  } catch (err) {
173
202
  // If the local HEAD is a commit that has not been pushed to the remote, the above command will fail.
174
203
  // eslint-disable-next-line eslint-rules/eslint-log-printf-style
@@ -177,15 +206,29 @@ function unshallowRepository (parentOnly = false) {
177
206
  TELEMETRY_GIT_COMMAND_ERRORS,
178
207
  { command: 'unshallow', errorType: err.code, exitCode: err.status || err.errno }
179
208
  )
209
+ if (targetedRecovery) {
210
+ const timeout = 5000 - (Date.now() - start)
211
+ if (timeout > 0 && !hasLocalGitObject(revParseHead, cwd)) {
212
+ sanitizedExec(
213
+ ['fetch', '--depth=1', ...baseGitOptions.slice(2), revParseHead],
214
+ null, null,
215
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'unshallow' } },
216
+ true,
217
+ { uncached: true, timeout, expectedFailure: true, cwd }
218
+ )
219
+ }
220
+ return
221
+ }
180
222
  const upstreamRemote = sanitizedExec(
181
- ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}']
223
+ ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'],
224
+ null, null, null, true, { uncached: true, cwd }
182
225
  )
183
226
  flags = [
184
227
  ...baseGitOptions,
185
228
  upstreamRemote,
186
229
  ]
187
230
  try {
188
- cachedExec('git', flags)
231
+ execFileSync('git', flags, fetchOptions)
189
232
  } catch (err) {
190
233
  // If the CI is working on a detached HEAD or branch tracking hasn't been set up, the above command will fail.
191
234
  // eslint-disable-next-line eslint-rules/eslint-log-printf-style
@@ -199,34 +242,56 @@ function unshallowRepository (parentOnly = false) {
199
242
  baseGitOptions,
200
243
  null,
201
244
  null,
202
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'unshallow' } } // we log the error in sanitizedExec
245
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'unshallow' } }, // we log the error in sanitizedExec
246
+ true,
247
+ { uncached: true, cwd }
203
248
  )
204
249
  }
205
250
  }
206
251
  distributionMetric(TELEMETRY_GIT_COMMAND_MS, { command: 'unshallow' }, Date.now() - start)
207
252
  }
208
253
 
209
- function getRepositoryUrl () {
254
+ function getRepositoryUrl (cwd) {
210
255
  return sanitizedExec(
211
- ['config', '--get', 'remote.origin.url'],
256
+ ['remote', 'get-url', getGitRemoteName(cwd)],
212
257
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_repository' } },
213
258
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_repository' } },
214
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_repository' } }
259
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_repository' } },
260
+ true,
261
+ { uncached: true, cwd }
215
262
  )
216
263
  }
217
264
 
218
- function getLatestCommits () {
265
+ /**
266
+ * @param {string=} commitSha
267
+ * @param {string=} cwd
268
+ */
269
+ function getLatestCommits (commitSha, cwd) {
270
+ if (commitSha && !validateGitCommitSha(commitSha)) return []
219
271
  incrementCountMetric(TELEMETRY_GIT_COMMAND, { command: 'get_local_commits' })
220
272
  const startTime = Date.now()
221
273
  try {
222
- const result = cachedExec('git', ['log', '--format=%H', '-n 1000', '--since="1 month ago"'])
274
+ const flags = ['log', '--format=%H', '-n 1000', '--since="1 month ago"']
275
+ if (commitSha) flags.push(commitSha, '--')
276
+ const env = { ...getGitEnvironment(), GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' }
277
+ /** @type {import('node:child_process').ExecFileSyncOptionsWithBufferEncoding} */
278
+ const options = { stdio: 'pipe', env }
279
+ if (cwd) options.cwd = cwd
280
+ const result = execFileSync('git', flags, options)
223
281
  .toString()
224
282
  .split('\n')
225
283
  .filter(Boolean)
284
+ if (commitSha && !result.includes(commitSha)) {
285
+ const selectedCommit = execFileSync('git',
286
+ ['rev-parse', '--verify', commitSha + '^{commit}'], options)
287
+ .toString().trim()
288
+ if (selectedCommit !== commitSha) return []
289
+ result.unshift(commitSha)
290
+ }
226
291
  distributionMetric(TELEMETRY_GIT_COMMAND_MS, { command: 'get_local_commits' }, Date.now() - startTime)
227
292
  return result
228
293
  } catch (err) {
229
- log.error('Get latest commits failed: %s', err.message)
294
+ log.error('Get latest commits failed', err)
230
295
  incrementCountMetric(
231
296
  TELEMETRY_GIT_COMMAND_ERRORS,
232
297
  { command: 'get_local_commits', errorType: err.status }
@@ -235,12 +300,12 @@ function getLatestCommits () {
235
300
  }
236
301
  }
237
302
 
238
- function getGitDiff (baseCommit, targetCommit) {
303
+ function getGitDiff (baseCommit, targetCommit, cwd) {
239
304
  // Fetch the required snapshots explicitly; a depth-1 metadata fetch does not provide their ancestry.
240
305
  for (const commit of targetCommit ? [baseCommit, targetCommit] : [baseCommit]) {
241
- if (!hasLocalGitObject(commit)) {
306
+ if (!hasLocalGitObject(commit, cwd)) {
242
307
  // Use the current checkout state so a stale cache cannot shorten a complete clone.
243
- fetchHeadCommitSha(commit, isShallowRepository(true))
308
+ fetchHeadCommitSha(commit, isShallowRepository(true, cwd), cwd)
244
309
  }
245
310
  }
246
311
  const flags = ['diff', '-U0', '--word-diff=porcelain', baseCommit]
@@ -253,49 +318,90 @@ function getGitDiff (baseCommit, targetCommit) {
253
318
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'diff' } },
254
319
  { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'diff' } },
255
320
  false, // important not to trim or we'll lose the line breaks which we need to detect impacted tests
256
- { uncached: true }
321
+ { uncached: true, cwd }
257
322
  )
258
323
  }
259
324
 
260
- function getGitRemoteName () {
261
- const upstreamRemote = sanitizedExec(
262
- ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'],
325
+ function getGitRemoteName (cwd) {
326
+ const branchRef = sanitizedExec(
327
+ ['symbolic-ref', '--quiet', 'HEAD'],
263
328
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_remote_name' } },
264
329
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_remote_name' } },
265
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } }
330
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } },
331
+ true,
332
+ { expectedFailure: true, uncached: true, cwd } // Detached HEADs use the checkout-local fallback.
266
333
  )
267
334
 
268
- if (upstreamRemote) {
269
- return getSegment(upstreamRemote, '/', 0)
270
- }
271
-
272
335
  const remotes = sanitizedExec(
273
336
  ['remote'],
274
337
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_remote_name' } },
275
338
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_remote_name' } },
276
339
  { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } },
277
- false
340
+ false,
341
+ { uncached: true, cwd }
342
+ )
343
+
344
+ if (!remotes) return 'origin'
345
+
346
+ const remoteNames = remotes.trim().split(/\r?\n/)
347
+ const branch = branchRef.replace(/^refs\/heads\//, '')
348
+ if (branch) {
349
+ // Read the configured remote: custom fetch refspecs can use any ref namespace.
350
+ const branchRemote = sanitizedExec(
351
+ ['config', '--get', `branch.${branch}.remote`],
352
+ { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_remote_name' } },
353
+ { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_remote_name' } },
354
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } },
355
+ true,
356
+ { expectedFailure: true, uncached: true, cwd }
357
+ )
358
+ if (branchRemote !== '.' && remoteNames.includes(branchRemote)) return branchRemote
359
+ }
360
+ const configuredRemote = sanitizedExec(
361
+ ['config', '--local', '--get', 'clone.defaultRemoteName'],
362
+ { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_remote_name' } },
363
+ { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_remote_name' } },
364
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } },
365
+ true,
366
+ { expectedFailure: true, uncached: true, cwd }
278
367
  )
279
368
 
280
- return getSegment(remotes, '\n', 0) || 'origin'
369
+ if (configuredRemote && remoteNames.includes(configuredRemote)) return configuredRemote
370
+ // Clone -o overrides remain in local branch tracking after HEAD is detached.
371
+ const trackingConfiguration = sanitizedExec(
372
+ ['config', '--local', '--get-regexp', String.raw`^branch\..*\.remote$`],
373
+ { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_remote_name' } },
374
+ { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_remote_name' } },
375
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_remote_name' } },
376
+ false,
377
+ { expectedFailure: true, uncached: true, cwd }
378
+ )
379
+ const trackingRemotes = new Set(trackingConfiguration.split(/\r?\n/)
380
+ .map(line => getSegment(line, ' ', 1)).filter(remote => remote && remoteNames.includes(remote)))
381
+ if (trackingRemotes.size === 1) return trackingRemotes.values().next().value
382
+ return remoteNames.includes('origin') ? 'origin' : remoteNames[0] || 'origin'
281
383
  }
282
384
 
283
- function getSourceBranch () {
385
+ function getSourceBranch (cwd) {
284
386
  return sanitizedExec(
285
387
  ['rev-parse', '--abbrev-ref', 'HEAD'],
286
388
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_source_branch' } },
287
389
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_source_branch' } },
288
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_source_branch' } }
390
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_source_branch' } },
391
+ true, { cwd }
289
392
  )
290
393
  }
291
394
 
292
- function checkAndFetchBranch (branch, remoteName) {
395
+ function checkAndFetchBranch (branch, remoteName, cwd) {
396
+ const options = { stdio: 'pipe', env: getGitEnvironment() }
397
+ if (cwd) options.cwd = cwd
293
398
  try {
294
399
  // `git show-ref --verify --quiet refs/remotes/${remoteName}/${branch}` will exit 0 if the branch exists
295
400
  // Otherwise it will exit 1
296
401
  cachedExec(
297
402
  'git',
298
403
  ['show-ref', '--verify', '--quiet', `refs/remotes/${remoteName}/${branch}`],
404
+ options
299
405
  )
300
406
  // branch exists locally, so we finish
301
407
  } catch {
@@ -308,14 +414,14 @@ function checkAndFetchBranch (branch, remoteName) {
308
414
  const remoteHeads = cachedExec(
309
415
  'git',
310
416
  ['ls-remote', '--heads', remoteName, branch],
311
- { stdio: 'pipe', timeout: 2000 }
417
+ { ...options, timeout: 2000 }
312
418
  )
313
419
  if (remoteHeads) {
314
420
  // branch exists, so we'll fetch it
315
421
  cachedExec(
316
422
  'git',
317
423
  ['fetch', '--depth', '1', remoteName, branch],
318
- { stdio: 'pipe', timeout: 5000 }
424
+ { ...options, timeout: 5000 }
319
425
  )
320
426
  }
321
427
  } catch (err) {
@@ -325,13 +431,13 @@ function checkAndFetchBranch (branch, remoteName) {
325
431
  }
326
432
  }
327
433
 
328
- function getLocalBranches (remoteName) {
434
+ function getLocalBranches (remoteName, cwd) {
329
435
  const localBranches = sanitizedExec(
330
436
  ['for-each-ref', '--format=%(refname:short)', `refs/remotes/${remoteName}`],
331
437
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_local_branches' } },
332
438
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_local_branches' } },
333
439
  { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_local_branches' } },
334
- false
440
+ false, { cwd }
335
441
  )
336
442
  try {
337
443
  return localBranches.split('\n').filter(Boolean)
@@ -340,21 +446,23 @@ function getLocalBranches (remoteName) {
340
446
  }
341
447
  }
342
448
 
343
- function getMergeBase (baseBranch, sourceBranch) {
449
+ function getMergeBase (baseBranch, sourceBranch, cwd) {
344
450
  return sanitizedExec(
345
451
  ['merge-base', baseBranch, sourceBranch],
346
452
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_merge_base' } },
347
453
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_merge_base' } },
348
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_merge_base' } }
454
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_merge_base' } },
455
+ true, { cwd }
349
456
  )
350
457
  }
351
458
 
352
- function getCounts (sourceBranch, candidateBranch) {
459
+ function getCounts (sourceBranch, candidateBranch, cwd) {
353
460
  const counts = sanitizedExec(
354
461
  ['rev-list', '--left-right', '--count', `${candidateBranch}...${sourceBranch}`],
355
462
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_counts' } },
356
463
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_counts' } },
357
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_counts' } }
464
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_counts' } },
465
+ true, { cwd }
358
466
  )
359
467
  try {
360
468
  if (!counts) {
@@ -367,15 +475,28 @@ function getCounts (sourceBranch, candidateBranch) {
367
475
  }
368
476
  }
369
477
 
370
- function getCommitsRevList (commitsToExclude, commitsToInclude) {
371
- let result = null
478
+ /**
479
+ * @param {string[]} commitsToExclude
480
+ * @param {string[]} commitsToInclude
481
+ * @param {boolean} [allowLazyFetch] Allow a bounded retry to hydrate promised objects.
482
+ * @param {string=} cwd
483
+ */
484
+ function getCommitsRevList (commitsToExclude, commitsToInclude, allowLazyFetch = false, cwd) {
485
+ let result
372
486
 
373
487
  const commitsToExcludeString = commitsToExclude.map(commit => `^${commit}`)
374
488
 
375
489
  incrementCountMetric(TELEMETRY_GIT_COMMAND, { command: 'get_objects' })
376
490
  const startTime = Date.now()
377
491
  try {
378
- result = cachedExec(
492
+ const env = allowLazyFetch
493
+ ? getGitEnvironment()
494
+ : { ...getGitEnvironment(), GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' }
495
+ /** @type {import('node:child_process').ExecFileSyncOptions} */
496
+ const options = { stdio: 'pipe', maxBuffer: GIT_REV_LIST_MAX_BUFFER, env }
497
+ if (cwd) options.cwd = cwd
498
+ if (allowLazyFetch) options.timeout = 5000
499
+ result = execFileSync(
379
500
  'git',
380
501
  [
381
502
  'rev-list',
@@ -383,15 +504,32 @@ function getCommitsRevList (commitsToExclude, commitsToInclude) {
383
504
  '--no-object-names',
384
505
  '--filter=blob:none',
385
506
  '--since="1 month ago"',
507
+ '--max-count=1000',
386
508
  ...commitsToExcludeString,
387
509
  ...commitsToInclude,
388
510
  ],
389
- { stdio: 'pipe', maxBuffer: GIT_REV_LIST_MAX_BUFFER })
511
+ options)
390
512
  .toString()
391
513
  .split('\n')
392
514
  .filter(Boolean)
515
+ const objects = new Set(result)
516
+ // Only the first root can be the old selection prepended by getLatestCommits; other roots came from its window.
517
+ const selectedCommit = commitsToInclude[0]
518
+ if (validateGitCommitSha(selectedCommit) && !objects.has(selectedCommit) &&
519
+ !commitsToExclude.includes(selectedCommit)) {
520
+ if (allowLazyFetch) {
521
+ options.timeout = 5000 - (Date.now() - startTime)
522
+ if (options.timeout <= 0) throw new Error('Git object hydration timed out')
523
+ }
524
+ const selectedObjects = execFileSync('git', [
525
+ 'rev-list', '--objects', '--no-object-names', '--filter=blob:none', '--max-count=1',
526
+ ...commitsToExcludeString, selectedCommit,
527
+ ], options).toString().split('\n').filter(Boolean)
528
+ result = [...new Set([...result, ...selectedObjects])]
529
+ }
393
530
  } catch (err) {
394
- log.error('Get commits to upload failed: %s', err.message)
531
+ result = null
532
+ log.error('Get commits to upload failed', err)
395
533
  incrementCountMetric(
396
534
  TELEMETRY_GIT_COMMAND_ERRORS,
397
535
  { command: 'get_objects', errorType: err.code, exitCode: err.status || err.errno } // err.status might be null
@@ -401,7 +539,7 @@ function getCommitsRevList (commitsToExclude, commitsToInclude) {
401
539
  return result
402
540
  }
403
541
 
404
- function generatePackFilesForCommits (commitsToUpload) {
542
+ function generatePackFilesForCommits (commitsToUpload, cwd) {
405
543
  let result = []
406
544
  const tmpFolder = os.tmpdir()
407
545
 
@@ -413,13 +551,16 @@ function generatePackFilesForCommits (commitsToUpload) {
413
551
 
414
552
  const randomPrefix = String(Math.floor(Math.random() * 10_000))
415
553
  const temporaryPath = path.join(tmpFolder, randomPrefix)
416
- const cwdPath = path.join(process.cwd(), randomPrefix)
554
+ const cwdPath = path.join(cwd || process.cwd(), randomPrefix)
417
555
 
418
556
  incrementCountMetric(TELEMETRY_GIT_COMMAND, { command: 'pack_objects' })
419
557
  const startTime = Date.now()
420
558
  // Generates pack files to upload and
421
559
  // returns the ordered list of packfiles' paths
422
560
  function execGitPackObjects (targetPath) {
561
+ /** @type {import('node:child_process').ExecFileSyncOptionsWithBufferEncoding} */
562
+ const options = { stdio: 'pipe', input: commitsToUpload.join('\n'), env: getGitEnvironment() }
563
+ if (cwd) options.cwd = cwd
423
564
  return cachedExec(
424
565
  'git',
425
566
  [
@@ -428,14 +569,14 @@ function generatePackFilesForCommits (commitsToUpload) {
428
569
  '--max-pack-size=3m',
429
570
  targetPath,
430
571
  ],
431
- { stdio: 'pipe', input: commitsToUpload.join('\n') }
572
+ options
432
573
  ).toString().split('\n').filter(Boolean).map(commit => `${targetPath}-${commit}.pack`)
433
574
  }
434
575
 
435
576
  try {
436
577
  result = execGitPackObjects(temporaryPath)
437
578
  } catch (err) {
438
- log.error('Git plugin error executing git pack-objects command', err)
579
+ log.debug('Git plugin error executing git pack-objects command, trying the working directory', err)
439
580
  incrementCountMetric(
440
581
  TELEMETRY_GIT_COMMAND_ERRORS,
441
582
  { command: 'pack_objects', exitCode: err.status || err.errno, errorType: err.code }
@@ -467,13 +608,22 @@ function generatePackFilesForCommits (commitsToUpload) {
467
608
  return result
468
609
  }
469
610
 
470
- function getRepositoryRoot () {
611
+ function getRepositoryRoot (cwd) {
471
612
  return sanitizedExec(
472
- ['rev-parse', '--show-toplevel']
613
+ ['rev-parse', '--show-toplevel'], null, null, null, true, { uncached: true, cwd }
473
614
  )
474
615
  }
475
616
 
476
- // If there is ciMetadata, it takes precedence.
617
+ /**
618
+ * Read mutable checkout metadata without the cache shared by CI jobs.
619
+ * @param {string[]} flags
620
+ * @param {boolean} [shouldTrim]
621
+ * @param {string=} cwd
622
+ */
623
+ function readCheckout (flags, shouldTrim = true, cwd) {
624
+ return sanitizedExec(flags, null, null, null, shouldTrim, { uncached: true, cwd })
625
+ }
626
+
477
627
  function getGitMetadata (ciMetadata) {
478
628
  const {
479
629
  commitSHA,
@@ -487,6 +637,7 @@ function getGitMetadata (ciMetadata) {
487
637
  headCommitSha,
488
638
  pullRequestBaseBranchSha,
489
639
  } = ciMetadata
640
+ const gitRoot = getRepositoryRoot()
490
641
 
491
642
  // With stdio: 'pipe', errors in this command will not be output to the parent process,
492
643
  // so if `git` is not present in the env, we won't show a warning to the user.
@@ -497,36 +648,63 @@ function getGitMetadata (ciMetadata) {
497
648
  committerName,
498
649
  committerEmail,
499
650
  committerDate,
500
- ] = sanitizedExec(['show', '-s', '--format=%an,%ae,%aI,%cn,%ce,%cI']).split(',')
651
+ ] = readCheckout([
652
+ 'show', '-s', '--no-show-signature', '--format=%an%x00%ae%x00%aI%x00%cn%x00%ce%x00%cI',
653
+ ], true, gitRoot).split('\0')
501
654
 
502
655
  const tags = {
503
656
  [GIT_COMMIT_MESSAGE]:
504
- commitMessage || sanitizedExec(['show', '-s', '--format=%B'], null, null, null, false),
505
- [GIT_BRANCH]: branch || sanitizedExec(['rev-parse', '--abbrev-ref', 'HEAD']),
506
- [GIT_COMMIT_SHA]: commitSHA || sanitizedExec(['rev-parse', 'HEAD']),
507
- [CI_WORKSPACE_PATH]: ciWorkspacePath || getRepositoryRoot(),
657
+ commitMessage || readCheckout(['show', '-s', '--no-show-signature', '--format=%B'], false, gitRoot),
658
+ [GIT_BRANCH]: branch || readCheckout(['rev-parse', '--abbrev-ref', 'HEAD'], true, gitRoot),
659
+ [GIT_COMMIT_SHA]: commitSHA || readCheckout(['rev-parse', 'HEAD'], true, gitRoot),
660
+ [CI_WORKSPACE_PATH]: ciWorkspacePath || gitRoot,
661
+ }
662
+
663
+ if (gitRoot) {
664
+ tags[GIT_CHECKOUT_ROOT] = gitRoot
665
+ if (ciWorkspacePath) {
666
+ let workspacePath = path.resolve(ciWorkspacePath)
667
+ let checkoutPath = path.resolve(gitRoot)
668
+ try {
669
+ workspacePath = fs.realpathSync.native(workspacePath)
670
+ checkoutPath = fs.realpathSync.native(checkoutPath)
671
+ } catch {
672
+ // A provider may report a host path that is not mounted in the test container.
673
+ }
674
+ if (workspacePath !== checkoutPath) tags[GIT_CHECKOUT_IS_CUSTOM] = 'true'
675
+ }
676
+ }
677
+
678
+ if (tags[GIT_BRANCH] === 'HEAD') delete tags[GIT_BRANCH]
679
+
680
+ let localTag = tag
681
+ if (!localTag && !tags[GIT_BRANCH]) {
682
+ const tagsAtHead = readCheckout(['tag', '--no-column', '--points-at', 'HEAD'], false, gitRoot)
683
+ .trim().split(/\r?\n/).filter(Boolean)
684
+ if (tagsAtHead.length === 1) localTag = tagsAtHead[0]
508
685
  }
509
686
 
510
687
  if (headCommitSha) {
511
- const shallow = isShallowRepository(true)
512
- const fetchedAtDepthOne = shallow && fetchHeadCommitSha(headCommitSha)
688
+ const shallow = isShallowRepository(true, gitRoot)
689
+ const fetchedAtDepthOne = shallow && fetchHeadCommitSha(headCommitSha, true, gitRoot)
513
690
  const headFlags = [
514
691
  'show',
515
692
  '-s',
516
- '--format=\'%H","%aI","%an","%ae","%cI","%cn","%ce","%B\'',
693
+ '--no-show-signature',
694
+ '--format=%H%x00%aI%x00%an%x00%ae%x00%cI%x00%cn%x00%ce%x00%B',
517
695
  headCommitSha,
518
696
  ]
519
697
  // Only explicit, bounded fetches may contact the remote during head metadata recovery.
520
- const headOptions = { uncached: true, localOnly: true }
698
+ const headOptions = { uncached: true, localOnly: true, cwd: gitRoot }
521
699
  let headInfo = sanitizedExec(headFlags, null, null, null, false, headOptions)
522
700
  if (!headInfo && (fetchedAtDepthOne || !shallow)) {
523
- fetchHeadCommitSha(headCommitSha, false)
701
+ fetchHeadCommitSha(headCommitSha, false, gitRoot)
524
702
  headInfo = sanitizedExec(headFlags, null, null, null, false, headOptions)
525
703
  }
526
704
 
527
705
  // Prepare a known diff base while the remote is available, including when Git upload is disabled.
528
- if (shallow && pullRequestBaseBranchSha && !hasLocalGitObject(pullRequestBaseBranchSha)) {
529
- fetchHeadCommitSha(pullRequestBaseBranchSha)
706
+ if (shallow && pullRequestBaseBranchSha && !hasLocalGitObject(pullRequestBaseBranchSha, gitRoot)) {
707
+ fetchHeadCommitSha(pullRequestBaseBranchSha, true, gitRoot)
530
708
  }
531
709
 
532
710
  const [
@@ -538,7 +716,7 @@ function getGitMetadata (ciMetadata) {
538
716
  headCommitterName,
539
717
  headCommitterEmail,
540
718
  headCommitMessage,
541
- ] = headInfo.split('","')
719
+ ] = headInfo.split('\0')
542
720
 
543
721
  if (gitHeadCommitSha) {
544
722
  tags[GIT_COMMIT_HEAD_AUTHOR_DATE] = headAuthorDate
@@ -553,14 +731,14 @@ function getGitMetadata (ciMetadata) {
553
731
 
554
732
  const entries = [
555
733
  GIT_REPOSITORY_URL,
556
- filterSensitiveInfoFromRepository(repositoryUrl || sanitizedExec(['ls-remote', '--get-url'])),
734
+ filterSensitiveInfoFromRepository(repositoryUrl || getRepositoryUrl(gitRoot)),
557
735
  GIT_COMMIT_AUTHOR_DATE, authorDate,
558
736
  GIT_COMMIT_AUTHOR_NAME, ciAuthorName || authorName,
559
737
  GIT_COMMIT_AUTHOR_EMAIL, ciAuthorEmail || authorEmail,
560
738
  GIT_COMMIT_COMMITTER_DATE, committerDate,
561
739
  GIT_COMMIT_COMMITTER_NAME, committerName,
562
740
  GIT_COMMIT_COMMITTER_EMAIL, committerEmail,
563
- GIT_TAG, tag,
741
+ GIT_TAG, localTag,
564
742
  ]
565
743
 
566
744
  for (let i = 0; i < entries.length; i += 2) {
@@ -573,28 +751,36 @@ function getGitMetadata (ciMetadata) {
573
751
  return tags
574
752
  }
575
753
 
576
- function getGitInformationDiscrepancy () {
577
- const gitRepositoryUrl = getRepositoryUrl()
754
+ function getGitInformationDiscrepancy (cwd) {
755
+ const gitRepositoryUrl = getRepositoryUrl(cwd)
578
756
 
579
757
  const gitCommitSHA = sanitizedExec(
580
758
  ['rev-parse', 'HEAD'],
581
759
  { name: TELEMETRY_GIT_COMMAND, tags: { command: 'get_commit_sha' } },
582
760
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'get_commit_sha' } },
583
- { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_commit_sha' } }
761
+ { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'get_commit_sha' } },
762
+ true,
763
+ { uncached: true, cwd }
584
764
  )
585
765
 
586
766
  return { gitRepositoryUrl, gitCommitSHA }
587
767
  }
588
768
 
589
- /** @param {string} sha */
590
- function hasLocalGitObject (sha) {
769
+ /**
770
+ * @param {string} sha
771
+ * @param {string=} cwd
772
+ */
773
+ function hasLocalGitObject (sha, cwd) {
591
774
  return legacyStorage.run({ noop: true }, () => {
592
775
  try {
593
- execFileSync('git', ['cat-file', '-e', sha], {
776
+ /** @type {import('node:child_process').ExecFileSyncOptionsWithBufferEncoding} */
777
+ const options = {
594
778
  stdio: 'pipe',
595
779
  // Older Git versions ignore GIT_NO_LAZY_FETCH, so also disallow transports for this local-only probe.
596
- env: { ...getEnvironmentVariables(), GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' },
597
- })
780
+ env: { ...getGitEnvironment(), GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' },
781
+ }
782
+ if (cwd) options.cwd = cwd
783
+ execFileSync('git', ['cat-file', '-e', sha], options)
598
784
  return true
599
785
  } catch {
600
786
  return false
@@ -602,10 +788,10 @@ function hasLocalGitObject (sha) {
602
788
  })
603
789
  }
604
790
 
605
- function fetchHeadCommitSha (headSha, useDepth = true) {
606
- const remoteName = getGitRemoteName()
791
+ function fetchHeadCommitSha (headSha, useDepth = true, cwd) {
792
+ const remoteName = getGitRemoteName(cwd)
607
793
  // Existing heads retain the original repair/history behavior; a failed bounded fetch also retries that path.
608
- const depths = useDepth && !hasLocalGitObject(headSha) ? [true, false] : [false]
794
+ const depths = useDepth && !hasLocalGitObject(headSha, cwd) ? [true, false] : [false]
609
795
  for (const shallow of depths) {
610
796
  try {
611
797
  sanitizedExec(
@@ -623,7 +809,7 @@ function fetchHeadCommitSha (headSha, useDepth = true) {
623
809
  { name: TELEMETRY_GIT_COMMAND_MS, tags: { command: 'fetch_head_commit_sha' } },
624
810
  { name: TELEMETRY_GIT_COMMAND_ERRORS, tags: { command: 'fetch_head_commit_sha' } },
625
811
  true,
626
- { uncached: true, throwOnError: true, timeout: 5000 }
812
+ { uncached: true, throwOnError: true, timeout: 5000, cwd }
627
813
  )
628
814
  return shallow
629
815
  } catch {
@@ -651,6 +837,7 @@ module.exports = {
651
837
  getLocalBranches,
652
838
  getMergeBase,
653
839
  getCounts,
840
+ hasLocalGitObject,
654
841
  fetchHeadCommitSha,
655
842
  getRepositoryRoot,
656
843
  }