<commit_before>import { ensureDir, outputFile, readFile } from 'fs-extra';
import { join, dirname } from 'upath';
import { exec } from '../../util/exec';
import { find } from '../../util/host-rules';
import { getChildProcessEnv } from '../../util/env';
import { logger } from '../../logger';
import { UpdateArtifactsConfig, UpdateArtifactsResult } from '../common';
import { platform } from '../../platform';

export async function updateArtifacts(
  goModFileName: string,
  _updatedDeps: string[],
  newGoModContent: string,
  config: UpdateArtifactsConfig
): Promise<UpdateArtifactsResult[] | null> {
  logger.debug(`gomod.updateArtifacts(${goModFileName})`);
  process.env.GOPATH =
    process.env.GOPATH || join(config.cacheDir, './others/go');
  await ensureDir(process.env.GOPATH);
  logger.debug('Using GOPATH: ' + process.env.GOPATH);
  const sumFileName = goModFileName.replace(/\.mod$/, '.sum');
  const existingGoSumContent = await platform.getFile(sumFileName);
  if (!existingGoSumContent) {
    logger.debug('No go.sum found');
    return null;
  }
  const cwd = join(config.localDir, dirname(goModFileName));
  let stdout: string;
  let stderr: string;
  try {
    const localGoModFileName = join(config.localDir, goModFileName);
    const massagedGoMod = newGoModContent.replace(
      /\n(replace\s+[^\s]+\s+=>\s+\.\.\/.*)/g,
      '\n// renovate-replace $1'
    );
    if (massagedGoMod !== newGoModContent) {
      logger.debug('Removed some relative replace statements from go.mod');
    }
    await outputFile(localGoModFileName, massagedGoMod);
    const localGoSumFileName = join(config.localDir, sumFileName);
    const customEnv = ['GOPATH', 'GOPROXY'];
    const env = getChildProcessEnv(customEnv);
    const startTime = process.hrtime();
    let cmd: string;
    if (config.binarySource === 'docker') {
      logger.info('Running go via docker');
      cmd = `docker run --rm `;
      const volumes = [config.localDir, process.env.GOPATH];
      cmd += volumes.map(v => `-v ${v}:${v} `).join('');
      const envVars = customEnv;
      cmd += envVars.map(e => `-e ${e} `).join('');
      cmd += '-e CGO_ENABLED=0 ';
      cmd += `-w ${cwd} `;
      cmd += `renovate/go `;
      const credentials = find({
        hostType: 'github',
        url: 'https://api.github.com/',
      });
      if (credentials && credentials.token) {
        logger.debug('Setting github.com credentials');
        cmd += `bash -c "git config --global url.\\"https://${
          global.appMode
            ? `x-access-token:${credentials.token}`
            : credentials.token
        }@github.com/\\".insteadOf \\"https://github.com/\\" && go`;
      } else {
        cmd += 'go';
      }
    } else {
      logger.info('Running go via global command');
      cmd = 'go';
    }
    let args = 'get';
    if (cmd.includes('.insteadOf')) {
      args += '"';
    }
    logger.debug({ cmd, args }, 'go get command');
    ({ stdout, stderr } = await exec(`${cmd} ${args}`, {
      cwd,
      env,
    }));
    let duration = process.hrtime(startTime);
    let seconds = Math.round(duration[0] + duration[1] / 1e9);
    logger.info(
      { seconds, type: 'go.sum', stdout, stderr },
      'Generated lockfile'
    );
    if (
      config.postUpdateOptions &&
      config.postUpdateOptions.includes('gomodTidy')
    ) {
      args = 'mod tidy';<commit_msg>fix(gomod): add ./... to go get command (#4483)<commit_after>import { ensureDir, outputFile, readFile } from 'fs-extra';
import { join, dirname } from 'upath';
import { exec } from '../../util/exec';
import { find } from '../../util/host-rules';
import { getChildProcessEnv } from '../../util/env';
import { logger } from '../../logger';
import { UpdateArtifactsConfig, UpdateArtifactsResult } from '../common';
import { platform } from '../../platform';

export async function updateArtifacts(
  goModFileName: string,
  _updatedDeps: string[],
  newGoModContent: string,
  config: UpdateArtifactsConfig
): Promise<UpdateArtifactsResult[] | null> {
  logger.debug(`gomod.updateArtifacts(${goModFileName})`);
  process.env.GOPATH =
    process.env.GOPATH || join(config.cacheDir, './others/go');
  await ensureDir(process.env.GOPATH);
  logger.debug('Using GOPATH: ' + process.env.GOPATH);
  const sumFileName = goModFileName.replace(/\.mod$/, '.sum');
  const existingGoSumContent = await platform.getFile(sumFileName);
  if (!existingGoSumContent) {
    logger.debug('No go.sum found');
    return null;
  }
  const cwd = join(config.localDir, dirname(goModFileName));
  let stdout: string;
  let stderr: string;
  try {
    const localGoModFileName = join(config.localDir, goModFileName);
    const massagedGoMod = newGoModContent.replace(
      /\n(replace\s+[^\s]+\s+=>\s+\.\.\/.*)/g,
      '\n// renovate-replace $1'
    );
    if (massagedGoMod !== newGoModContent) {
      logger.debug('Removed some relative replace statements from go.mod');
    }
    await outputFile(localGoModFileName, massagedGoMod);
    const localGoSumFileName = join(config.localDir, sumFileName);
    const customEnv = ['GOPATH', 'GOPROXY'];
    const env = getChildProcessEnv(customEnv);
    const startTime = process.hrtime();
    let cmd: string;
    if (config.binarySource === 'docker') {
      logger.info('Running go via docker');
      cmd = `docker run --rm `;
      const volumes = [config.localDir, process.env.GOPATH];
      cmd += volumes.map(v => `-v ${v}:${v} `).join('');
      const envVars = customEnv;
      cmd += envVars.map(e => `-e ${e} `).join('');
      cmd += '-e CGO_ENABLED=0 ';
      cmd += `-w ${cwd} `;
      cmd += `renovate/go `;
      const credentials = find({
        hostType: 'github',
        url: 'https://api.github.com/',
      });
      if (credentials && credentials.token) {
        logger.debug('Setting github.com credentials');
        cmd += `bash -c "git config --global url.\\"https://${
          global.appMode
            ? `x-access-token:${credentials.token}`
            : credentials.token
        }@github.com/\\".insteadOf \\"https://github.com/\\" && go`;
      } else {
        cmd += 'go';
      }
    } else {
      logger.info('Running go via global command');
      cmd = 'go';
    }
    let args = 'get ./...';
    if (cmd.includes('.insteadOf')) {
      args += '"';
    }
    logger.debug({ cmd, args }, 'go get command');
    ({ stdout, stderr } = await exec(`${cmd} ${args}`, {
      cwd,
      env,
    }));
    let duration = process.hrtime(startTime);
    let seconds = Math.round(duration[0] + duration[1] / 1e9);
    logger.info(
      { seconds, type: 'go.sum', stdout, stderr },
      'Generated lockfile'
    );
    if (
      config.postUpdateOptions &&
      config.postUpdateOptions.includes('gomodTidy')
    ) {
      args = 'mod tidy';
{'fraction_non_alphanumeric': 0.17440141885900087, 'fraction_numerical': 0.0023647650014779783, 'mean_word_length': 2.821005081874647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125057', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2164, 'n_words': 487}