File size: 2,261 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_env::{DotenvProcessEnv, EnvMap, ProcessEnv};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::issue::{IssueExt, StyledString};

use crate::ProcessEnvIssue;

#[turbo_tasks::value]
pub struct TryDotenvProcessEnv {
    dotenv: ResolvedVc<DotenvProcessEnv>,
    prior: ResolvedVc<Box<dyn ProcessEnv>>,
    path: FileSystemPath,
}

#[turbo_tasks::value_impl]
impl TryDotenvProcessEnv {
    #[turbo_tasks::function]
    pub async fn new(
        prior: ResolvedVc<Box<dyn ProcessEnv>>,
        path: FileSystemPath,
    ) -> Result<Vc<Self>> {
        let dotenv = DotenvProcessEnv::new(Some(*prior), path.clone())
            .to_resolved()
            .await?;
        Ok(TryDotenvProcessEnv {
            dotenv,
            prior,
            path,
        }
        .cell())
    }
}

#[turbo_tasks::value_impl]
impl ProcessEnv for TryDotenvProcessEnv {
    #[turbo_tasks::function]
    async fn read_all(&self) -> Result<Vc<EnvMap>> {
        let dotenv = self.dotenv;
        let prior = dotenv.read_prior();

        // Ensure prior succeeds. If it doesn't, then we don't want to attempt to read
        // the dotenv file (and potentially emit an Issue), just trust that the prior
        // will have emitted its own.
        prior.await?;

        let vars = dotenv.read_all_with_prior(prior);
        match vars.await {
            Ok(_) => Ok(vars),
            Err(e) => {
                // If parsing the dotenv file fails (but getting the prior value didn't), then
                // we want to emit an Issue and fall back to the prior's read.
                ProcessEnvIssue {
                    path: self.path.clone(),
                    // read_all_with_prior will wrap a current error with a context containing the
                    // failing file, which we don't really care about (we report the filepath as the
                    // Issue context, not the description). So extract the real error.
                    description: StyledString::Text(e.root_cause().to_string().into())
                        .resolved_cell(),
                }
                .resolved_cell()
                .emit();
                Ok(prior)
            }
        }
    }
}