Repository: Infisical/infisical
File: backend/src/services/secret-sync/secret-sync-queue.ts (TypeScript)

Code before:
      throw new Error(
        "Invalid Secret Sync source configuration: folder no longer exists. Please update source environment and secret path."
      );

    const importedSecrets = await SecretSyncFns.getSecrets(secretSync, {
      appConnectionDAL,
      kmsService,
      gatewayService,
      gatewayV2Service
    });

    if (!Object.keys(importedSecrets).length) return {};

    let invalidNameCount = 0;
    let errorMessage = "";

    for (const [key] of Object.entries(importedSecrets)) {
      const result = SecretNameSchema.safeParse(key);
      if (!result.success) {
        invalidNameCount += 1;
        if (errorMessage === "") errorMessage = result.error.issues[0]?.message;
      }
    }

    if (invalidNameCount > 0) {
      throw new Error(
        `Found ${invalidNameCount} invalid secret name${invalidNameCount === 1 ? "" : "s"}. ${errorMessage}`
      );
    }

    const importedSecretMap: TSecretMap = {};

    const secretMap = await $getInfisicalSecrets(secretSync, false);

    const secretsToCreate: Parameters<typeof $createManySecretsRawFn>[0]["secrets"] = [];
    const secretsToUpdate: Parameters<typeof $updateManySecretsRawFn>[0]["secrets"] = [];

    Object.entries(importedSecrets).forEach(([key, secretData]) => {
      const { value, comment = "", skipMultilineEncoding } = secretData;

      const secret = {
        secretName: key,
        secretValue: value,
        type: SecretType.Shared,
        secretComment: comment,
        skipMultilineEncoding: skipMultilineEncoding ?? undefined
      };

      if (Object.hasOwn(secretMap, key)) {
        // Only update secrets if the source value is not empty
        if (value && value !== secretMap[key].value) {

Review comment (bug):
let's throw a `SecretSyncError` here with `shouldRetry` set to `false`; explicit imports fail on first attempt but if you select import during creation it runs inside the sync job and will retry 5 times - using this error tells it not to retry  

Code after:
      throw new Error(
        "Invalid Secret Sync source configuration: folder no longer exists. Please update source environment and secret path."
      );

    const importedSecrets = await SecretSyncFns.getSecrets(secretSync, {
      appConnectionDAL,
      kmsService,
      gatewayService,
      gatewayV2Service
    });

    if (!Object.keys(importedSecrets).length) return {};

    let invalidNameCount = 0;
    let errorMessage = "";

    for (const [key] of Object.entries(importedSecrets)) {
      const result = SecretNameSchema.safeParse(key);
      if (!result.success) {
        invalidNameCount += 1;
        if (errorMessage === "") errorMessage = result.error.issues[0]?.message;
      }
    }

    if (invalidNameCount > 0) {
      throw new SecretSyncError({
        message: `Found ${invalidNameCount} invalid secret name${invalidNameCount === 1 ? "" : "s"}. ${errorMessage}`,
        shouldRetry: false
      });
    }

    const importedSecretMap: TSecretMap = {};

    const secretMap = await $getInfisicalSecrets(secretSync, false);

    const secretsToCreate: Parameters<typeof $createManySecretsRawFn>[0]["secrets"] = [];
    const secretsToUpdate: Parameters<typeof $updateManySecretsRawFn>[0]["secrets"] = [];

    Object.entries(importedSecrets).forEach(([key, secretData]) => {
      const { value, comment = "", skipMultilineEncoding } = secretData;

      const secret = {
        secretName: key,
        secretValue: value,
        type: SecretType.Shared,
        secretComment: comment,
        skipMultilineEncoding: skipMultilineEncoding ?? undefined
      };

      if (Object.hasOwn(secretMap, key)) {
        // Only update secrets if the source value is not empty