|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::ffi::OsStr; |
| 3 | +use std::fmt::{Display, Formatter, Result as FmtResult}; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +use anyhow::{bail, Context as _, Result}; |
| 7 | +use clap::Args; |
| 8 | + |
| 9 | +use crate::api::{Api, ChunkUploadCapability}; |
| 10 | +use crate::config::Config; |
| 11 | +use crate::constants::{DEFAULT_MAX_DIF_SIZE, DEFAULT_MAX_WAIT}; |
| 12 | +use crate::utils::chunks::{upload_chunked_objects, Assemblable, ChunkOptions, Chunked}; |
| 13 | +use crate::utils::dif::DifFile; |
| 14 | +use symbolic::common::ByteView; |
| 15 | +use symbolic::common::DebugId; |
| 16 | + |
| 17 | +struct DartSymbolMapObject<'a> { |
| 18 | + bytes: &'a [u8], |
| 19 | + name: &'a str, |
| 20 | + debug_id: DebugId, |
| 21 | +} |
| 22 | + |
| 23 | +impl<'a> AsRef<[u8]> for DartSymbolMapObject<'a> { |
| 24 | + fn as_ref(&self) -> &[u8] { |
| 25 | + self.bytes |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a> Display for DartSymbolMapObject<'a> { |
| 30 | + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { |
| 31 | + write!(f, "dartsymbolmap {}", self.name) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'a> Assemblable for DartSymbolMapObject<'a> { |
| 36 | + fn name(&self) -> Cow<'_, str> { |
| 37 | + Cow::Borrowed(self.name) |
| 38 | + } |
| 39 | + |
| 40 | + fn debug_id(&self) -> Option<DebugId> { |
| 41 | + Some(self.debug_id) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Args, Clone)] |
| 46 | +pub(crate) struct DartSymbolMapUploadArgs { |
| 47 | + #[arg(short = 'o', long = "org")] |
| 48 | + #[arg(help = "The organization ID or slug.")] |
| 49 | + pub(super) org: Option<String>, |
| 50 | + |
| 51 | + #[arg(short = 'p', long = "project")] |
| 52 | + #[arg(help = "The project ID or slug.")] |
| 53 | + pub(super) project: Option<String>, |
| 54 | + |
| 55 | + #[arg(value_name = "MAPPING")] |
| 56 | + #[arg( |
| 57 | + help = "Path to the dartsymbolmap JSON file (e.g. dartsymbolmap.json). Must be a JSON array of strings with an even number of entries (pairs)." |
| 58 | + )] |
| 59 | + pub(super) mapping: String, |
| 60 | + |
| 61 | + #[arg(value_name = "DEBUG_FILE")] |
| 62 | + #[arg( |
| 63 | + help = "Path to the corresponding debug file to extract the Debug ID from. The file must contain exactly one Debug ID." |
| 64 | + )] |
| 65 | + pub(super) debug_file: String, |
| 66 | +} |
| 67 | + |
| 68 | +pub(super) fn execute(args: DartSymbolMapUploadArgs) -> Result<()> { |
| 69 | + let mapping_path = &args.mapping; |
| 70 | + let debug_file_path = &args.debug_file; |
| 71 | + |
| 72 | + // Extract Debug ID(s) from the provided debug file |
| 73 | + let dif = DifFile::open_path(debug_file_path, None)?; |
| 74 | + let mut ids: Vec<DebugId> = dif.ids().filter(|id| !id.is_nil()).collect(); |
| 75 | + |
| 76 | + // Ensure a single, unambiguous Debug ID |
| 77 | + ids.sort(); |
| 78 | + ids.dedup(); |
| 79 | + match ids.len() { |
| 80 | + 0 => bail!( |
| 81 | + "No debug identifier found in the provided debug file ({}). Ensure the file contains an embedded Debug ID.", |
| 82 | + debug_file_path |
| 83 | + ), |
| 84 | + 1 => { |
| 85 | + let debug_id = ids.remove(0); |
| 86 | + |
| 87 | + // Validate the dartsymbolmap JSON: must be a JSON array of strings with even length |
| 88 | + let mapping_file_bytes = ByteView::open(mapping_path) |
| 89 | + .with_context(|| format!("Failed to read mapping file at {mapping_path}"))?; |
| 90 | + let mapping_entries: Vec<Cow<'_, str>> = |
| 91 | + serde_json::from_slice(mapping_file_bytes.as_ref()) |
| 92 | + .context("Invalid dartsymbolmap: expected a JSON array of strings")?; |
| 93 | + |
| 94 | + if mapping_entries.len() % 2 != 0 { |
| 95 | + bail!( |
| 96 | + "Invalid dartsymbolmap: expected an even number of entries, got {}", |
| 97 | + mapping_entries.len() |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + // Prepare upload object |
| 102 | + let file_name = Path::new(mapping_path) |
| 103 | + .file_name() |
| 104 | + .and_then(OsStr::to_str) |
| 105 | + .unwrap_or(mapping_path) |
| 106 | + ; |
| 107 | + |
| 108 | + let mapping_len = mapping_file_bytes.len(); |
| 109 | + let object = DartSymbolMapObject { |
| 110 | + bytes: mapping_file_bytes.as_ref(), |
| 111 | + name: file_name, |
| 112 | + debug_id, |
| 113 | + }; |
| 114 | + |
| 115 | + // Prepare chunked upload |
| 116 | + let api = Api::current(); |
| 117 | + // Resolve org and project like logs: prefer args, fallback to defaults |
| 118 | + let config = Config::current(); |
| 119 | + let (default_org, default_project) = config.get_org_and_project_defaults(); |
| 120 | + let org = args |
| 121 | + .org |
| 122 | + .as_ref() |
| 123 | + .or(default_org.as_ref()) |
| 124 | + .ok_or_else(|| anyhow::anyhow!( |
| 125 | + "No organization specified. Please specify an organization using the --org argument." |
| 126 | + ))?; |
| 127 | + let project = args |
| 128 | + .project |
| 129 | + .as_ref() |
| 130 | + .or(default_project.as_ref()) |
| 131 | + .ok_or_else(|| anyhow::anyhow!( |
| 132 | + "No project specified. Use --project or set a default in config." |
| 133 | + ))?; |
| 134 | + let chunk_upload_options = api |
| 135 | + .authenticated()? |
| 136 | + .get_chunk_upload_options(org)? |
| 137 | + .ok_or_else(|| anyhow::anyhow!( |
| 138 | + "server does not support chunked uploading. Please update your Sentry server." |
| 139 | + ))?; |
| 140 | + |
| 141 | + if !chunk_upload_options.supports(ChunkUploadCapability::DartSymbolMap) { |
| 142 | + bail!( |
| 143 | + "Server does not support uploading Dart symbol maps via chunked upload. Please update your Sentry server." |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + // Early file size check against server or default limits (same as debug files) |
| 148 | + let effective_max_file_size = if chunk_upload_options.max_file_size > 0 { |
| 149 | + chunk_upload_options.max_file_size |
| 150 | + } else { |
| 151 | + DEFAULT_MAX_DIF_SIZE |
| 152 | + }; |
| 153 | + |
| 154 | + if (mapping_len as u64) > effective_max_file_size { |
| 155 | + bail!( |
| 156 | + "The dartsymbolmap '{}' exceeds the maximum allowed size ({} bytes > {} bytes).", |
| 157 | + mapping_path, |
| 158 | + mapping_len, |
| 159 | + effective_max_file_size |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + let options = ChunkOptions::new(chunk_upload_options, org, project) |
| 164 | + .with_max_wait(DEFAULT_MAX_WAIT); |
| 165 | + |
| 166 | + let chunked = Chunked::from(object, options.server_options().chunk_size as usize)?; |
| 167 | + let (_uploaded, has_processing_errors) = upload_chunked_objects(&[chunked], options)?; |
| 168 | + if has_processing_errors { |
| 169 | + bail!("Some symbol maps did not process correctly"); |
| 170 | + } |
| 171 | + |
| 172 | + Ok(()) |
| 173 | + } |
| 174 | + _ => bail!( |
| 175 | + "Multiple debug identifiers found in the provided debug file ({}): {}. Please provide a file that contains a single Debug ID.", |
| 176 | + debug_file_path, |
| 177 | + ids.into_iter().map(|id| id.to_string()).collect::<Vec<_>>().join(", ") |
| 178 | + ), |
| 179 | + } |
| 180 | +} |
0 commit comments